简体   繁体   中英

Points on a circle, with limits. How To calculate without angle, but radius and centre point?

This is quite complicated to explain, so I will do my best, sorry if there is anything I missed out, let me know and I will rectify it.

My question is, I have been tasked to draw this shape,

新月
(source: learnersdictionary.com )

This is to be done using C++ to write code that will calculate the points on this shape.

Important details.

User Input - Centre Point (X, Y), number of points to be shown, Font Size (influences radius)

Output - List of co-ordinates on the shape.

The overall aim once I have the points is to put them into a graph on Excel and it will hopefully draw it for me, at the user inputted size!

I know that the maximum Radius is 165mm and the minimum is 35mm. I have decided that my base Font Size shall be 20. I then did some thinking and came up with the equation.

Radius = (Chosen Font Size/20)*130. This is just an estimation, I realise it probably not right, but I thought it could work at least as a template.

I then decided that I should create two different circles, with two different centre points, then link them together to create the shape. I thought that the INSIDE line will have to have a larger Radius and a centre point further along the X-Axis (Y staying constant), as then it could cut into the outside line.

So I defined 2nd Centre point as (X+4, Y). (Again, just estimation, thought it doesn't really matter how far apart they are).

I then decided Radius 2 = (Chosen Font Size/20)*165 (max radius)

So, I have my 2 Radii, and two centre points.

Now to calculate the points on the circles, I am really struggling. I decided the best way to do it would be to create an increment (here is template)

for(int i=0; i<=n; i++) //where 'n' is users chosen number of points
{ 
  //Equation for X point
  //Equation for Y Point
  cout<<"("<<X<<","<<Y<<")"<<endl;
}

Now, for the life of me, I cannot figure out an equation to calculate the points. I have found equations that involve angles , but as I do not have any, I'm struggling.

I am, in essence, trying to calculate Point 'P' here, except all the way round the circle. 圆点
(source: tutorvista.com )

Another point I am thinking may be a problem is imposing limits on the values calculated to only display the values that are on the shape.? Not sure how to chose limits exactly other than to make the outside line a full Half Circle so I have a maximum radius?

So. Does anyone have any hints/tips/links they can share with me on how to proceed exactly?

Thanks again, any problems with the question, sorry will do my best to rectify if you let me know.

Cheers

UPDATE;

R1 = (Font/20)*130;
R2 = (Font/20)*165;

for(X1=0; X1<=n; X1++)
{
    Y1 = ((2*Y)+(pow(((4*((pow((X1-X), 2)))+(pow(R1, 2)))), 0.5)))/2;
    Y2 = ((2*Y)-(pow(((4*((pow((X1-X), 2)))+(pow(R1, 2)))), 0.5)))/2;
    cout<<"("<<X1<<","<<Y1<<")";
    cout<<"("<<X1<<","<<Y2<<")";
}

Opinion?

The equation of a circle is

(x - h)^2 + (y - k)^2 = r^2

With a little bit of algebra, you can iterate x over the range from h to h+r incrementing by some appropriate delta and calculate the two corresponding values of y. This will draw a complete circle.

The next step is to find the x-coordinate for the intersection of the two circles (assuming that the moon shape is defined by two appropriate circles). Again, some algebra and a pencil and paper will help.

More details:

To draw a circle without using polar coordinates and trig, you can do something like this:

for x in h-r to h+r increment by delta
    calculate both y coordinates

To calculate the y-coordinates, you need to solve the equation of a circle for y. The easiest way to do this is to transform it into a quadratic equation of the form A*y^2+B*y+C=0 and use the quadratic equation:

(x - h)^2 + (y - k)^2 = r^2
(x - h)^2 + (y - k)^2 - r^2 = 0
(y^2 - 2*k*y + k^2) + (x - h)^2  - r^2 = 0
y^2 - 2*k*y + (k^2 + (x - h)^2  - r^2) = 0

So we have

A = 1
B = -2*k
C = k^2 + (x - h)^2  - r^2

Now plug these into the quadratic equation and chug out the two y-values for each x value in the for loop. (Most likely, you will want to do the calculations in a separate function -- or functions.)

As you can see this is pretty messy. Doing this with trigonometry and angles will be much cleaner.

More thoughts:

Even though there are no angles in the user input described in the question, there is no intrinsic reason why you cannot use them during calculations (unless you have a specific requirement otherwise, say because your teacher told you not to). With that said, using polar coordinates makes this much easier. For a complete circle you can do something like this:

for theta = 0 to 2*PI increment by delta
    x = r * cos(theta)
    y = r * sin(theta)

To draw an arc, rather than a full circle, you simply change the limits for theta in the for loop. For example, the left-half of the circle goes from PI/2 to 3*PI/2 .

As per Code-Guru's comments on the question, the inner circle looks more like a half circle than the outer. Use the equation in Code-Guru's answer to calculate the points for the inner circle. Then, have a look at this question for how to calculate the radius of a circle which intersects your circle, given the distance (which you can set arbitrarily) and the points of intersection (which you know, because it's a half circle). From this you can draw the outer arc for any given distance, and all you need to do is vary the distance until you produce a shape that you're happy with.

This question may help you to apply Code-Guru's equation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM