Pages2

Sunday, April 12, 2015

Find points on circle in C++


1. Theory


2. Code

#include <iostream>
#include <math.h>
using namespace std;


int main ()
{
double r = 7; // put the value of radius of circle
double h = 2, k = 3; // (h,k) = center of circle coordinate
double x = 0, y = 0; // (x,y) = circle coordinate
double theta = 0; // in radians, start with 0 rad

for(int i = 0; i < 80 ;i++) // Pick 40 points or more (
{
x = (cos(theta) * r); // x value, start value is equal with r (cos (0) = 1)
y = sqrt(abs(r*r - x*x)); // Determine y value

if (theta > 3.1415926535897932384626433832795) // Add offset with center of circle, PI is C&P from Calculator
{ x = x + h; y = -y + k;}
else
{ x = x + h; y = y + k; }
cout  << x << ";" << y << endl;
theta = theta + 0.07853975; // Next x value (PI/40 or somethings else)
}

system("PAUSE");
return 0;
}


3. Sample Console Output

9;3
8.97842;3.54921
8.91382;4.09504
8.80659;4.63412
8.6574;5.16312
8.46716;5.67878
8.23705;6.17793
7.96848;6.65749
7.66312;7.11449
7.32284;7.54613
6.94975;7.94974
6.54614;8.32284
6.1145;8.66312
5.6575;8.96848
5.17794;9.23704
4.67879;9.46715
4.16313;9.65739
3.63413;9.80659
3.09505;9.91382
2.54922;9.97842
2.00001;10
1.4508;9.97842
0.904969;9.91382
0.365893;9.80659
-0.163108;9.6574
-0.678773;9.46716
-1.17792;9.23705
-1.65748;8.96849
-2.11449;8.66313
-2.54613;8.32285
-2.94974;7.94976
-3.32283;7.54615
-3.66311;7.11451
-3.96847;6.6575
-4.23704;6.17795
-4.46715;5.6788
-4.65739;5.16313
-4.80659;4.63413
-4.91382;4.09506
-4.97842;3.54923
-5;3.00002
-4.97842;2.45081
-4.91382;1.90498
-4.80659;1.3659
-4.6574;0.8369
-4.46716;0.321235
-4.23706;-0.177914
-3.96849;-0.657471
-3.66313;-1.11448
-3.32286;-1.54612
-2.94976;-1.94973
-2.54615;-2.32283
-2.11452;-2.6631
-1.65751;-2.96847
-1.17796;-3.23703
-0.678808;-3.46715
-0.163144;-3.65739
0.365857;-3.80658
0.904932;-3.91381
1.45076;-3.97842
1.99997;-4
2.54919;-3.97842
3.09501;-3.91382
3.63409;-3.8066
4.16309;-3.6574
4.67876;-3.46717
5.17791;-3.23706
5.65746;-2.9685
6.11447;-2.66314
6.54611;-2.32286
6.94972;-1.94977
7.32282;-1.54616
7.6631;-1.11452
7.96846;-0.657519
8.23703;-0.177964
8.46714;0.321184
8.65738;0.836847
8.80658;1.36585
8.91381;1.90492
8.97842;2.45075
Press any key to continue . . .


4. Plotting


No comments:

Post a Comment