Find the point on a circle with given center point, radius, and degree

后端 未结 10 1825
后悔当初
后悔当初 2020-12-02 06:57

It\'s been 10 years since I did any math like this... I am programming a game in 2D and moving a player around. As I move the player around I am trying to calculate the poin

10条回答
  •  攒了一身酷
    2020-12-02 07:48

    I wanted to share how your contributions above helped me produce an Arduino LCD compass. I hope this is the right etiquette...I just joined stackoverflow so I could thank you fine folks.

    While standing on the shoulders of the geometry giants above I was able to produce this sample compass: Arduino TFT compass with multiple bearings

    The code for the function I called repeatedly (for different bearings you see in tiny yellow text) is written in Arduino (kinda like "C")...and is pretty translatable:

    void PaintCompassNeedle( int pBearingInDegrees, int pRadius, TSPoint pCentrePt ) {
        // ******************************************************************************
        // * Formula for finding pointX on the circle based on degrees around the circle:
        // * x_oncircle = x_origin + radius * cos (degrees * pi / 180)  
        // * y_oncircle = y_origin - radius * sin (degrees * pi / 180) //minus explained
        // * Thanks to folks at stackoverflow...standing on the shoulders of giants. :) 
    
        float bearingInRads = (pBearingInDegrees) * PI / 180; 
        // Degrees vs Rads...The math folks use Rads in their formulas
    
        // *******************************************************************
        // * bearingPt is the point on the circle that we are trying to find
        TSPoint bearingPt;
        // Find the X on the circle starting with orgin (centre)
        bearingPt.x = pCentrePt.x + pRadius * sin(bearingInRads); 
        // Notice the "minus" R * cos()...because TFT the y is upside down bearingPt.y = 
        pCentrePt.y - pRadius * cos(bearingInRads); 
        // * Extra Explanation: The TFT is the graphical display I'm using and it
        // * calculates x & y from the top left of screen (portrait mode) as (0,0)
        // * ...so by Subtracting from the Y orgin...I flip it vertically
        // * Other folks using x,y as increasing to the right and up respectively
        // * would keep the plus sign after the pCentrePt.y
        // *************************************************************************
    
        // ***************************************************************
        // * This part will change for the final product...but leaving
        // * it because when call numerous times it shows it working for
        // * a number of different quadrants (displaying yellow degrees text)
        tft.fillCircle( bearingPt.x, bearingPt.y, 5, RED); 
        tft.setCursor( bearingPt.x, bearingPt.y );
        tft.setTextSize( 1 );
        tft.setTextColor( YELLOW );
        tft.print( pBearingInDegrees );
    
        TSPoint innerPt;
        innerPt.x = pCentrePt.x + pRadius/2 * sin(bearingInRads);
        innerPt.y = pCentrePt.y - pRadius/2 * cos(bearingInRads);
        tft.drawLine(innerPt.x, innerPt.y, bearingPt.x, bearingPt.y, RED);
    
    }
    

提交回复
热议问题