LoginSignup
0
0

More than 5 years have passed since last update.

Basic math for drawing a circle

Last updated at Posted at 2015-01-23

Radians
Basic:
- Radian is an unit for angles.
- 1 radian is the angle when the arc length is same as the radius
- Circumference is same as radians of 360 degrees = 2π
radians.jpg

180 degrees = 3.141592653589793 radians = PI
360 degrees = 6.283185307179586 radians = TWO_PI

1.0 radian = 180 / PI 
           = 57.29577951308232 degrees

Conversions between degrees and radians

DegToRad(degrees){
    radians = degrees * PI / 180
}

RadToDeg(radians){
    degrees = radians * 180 / PI
}

Trigonometry

In some programming languages, sin() or cos() function uses radians, not degrees for its argument.

sin(radians)
cos(radians)

Here are the values of sin or cos when 0, 90, 180, 270, 360 degrees.

//---use "signed long" type to get accurate values to avoid to be rounded

0° = 0 * PI/180 radians
sin(0) = 0
cos(0) = 1

90° = 90 * PI/180 radians
sin(PI / 2) = 1
cos(PI / 2) = 0

180° = 180 * PI/180 radians
sin(PI) = 0
cos(PI) = -1

270° = 270 * PI/180 radians
sin(PI * 3/2) = -1
cos(PI * 3/2) = 0

360° =  360 * PI/180 radians
sin(TWO_PI) = 0
cos(TWO_PI) = 1


Calculate a coordinate on a circle from an angle

sincos.jpg

x = cos(radians) * r
y = sin(radians) * r

or

x = cos(degrees * PI / 180) * r
y = sin(degrees * PI / 180) * r

Calculate a coordinate on a circle from a percentage
TWO_PI = 360° = 100%. You can calculate radians from a percentage.

percent_360.jpg

percentage = degrees / 360
x = cos(percentage * TWO_PI) * r
y = sin(percentage * TWO_PI) * r

The divisor is a number of vertices you want to set on a circle.
If the vertices is 10, the shape will be a decagon rather than a circle.
It's important when you want to get edges of the circle with for-loop etc.

percent_10.jpg

percentage = i / 10
x = radius * cos(percentage * TWO_PI) * r
y = radius * sin(percentage * TWO_PI) * r


atan2 returns an angle between two points in radians.

atan2.jpg

atan2(y2-y1, x2-x1)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0