Drawing a Crescent in a Few Easy Steps



Drawing a Crescent in a Few Easy Steps.

1. Notice that a crescent is completely defined by two circles:

|[pic] |[pic] |[pic] |

There you see three happy green crescents in a variety of shapes and orientations. In each case, the red circle defines the position and size of the crescent, and the blue circle defines the size and orientation of the “horns”.

As a circle is totally defined by the (x,y) position of its centre and its radius, all possible crescents can be defined by six numbers, specifying the centres and radii of the red and blue circles.

To make things simple, design your crescent-drawing class so that the user must provide those six numbers. Let the user do the work required to choose the correct values for the picture they have in mind.

2. How to draw a circle.

Remember the simple formula from trigonometry. If a circle has radius r and centre (cx,cy) then any point (px,py) on its circumference can be found if you know what direction it is in. Call the direction angle ( and we have:

[pic]

px = cx + r * cos(() and py = cy - r * sin(()

If you wonder why the formula for py has a minus instead of a +, just remember that computer graphics systems put the y axis upside-down.

So to draw a circle, just join together all the points on its circumference by constructing a loop that varies ( from 0( to 360( (but remember to use radians, so that becomes 0 to 2().

Loop, ( starts at 0, and ends at 2(, adding just a little bit each time round

{ px = cx + r * cos(()

py = cy - r * sin(()

if this is the first time round the loop

MoveTo(px, py);

else

DrawLineTo(px, py); }

2. How to draw just part of a circle.

If varying ( from 0 to 2( produces a whole circle, then varying it over a smaller range will just draw part of a circle. All you have to do is supply the starting and ending angles, and use them instead of 0 and 2( in the loop.

[pic]

But beware! you must ensure that your stopping angle is greater than your starting angle, otherwise nothing will come out. This can happen easily. If you want to draw the part of a circle that starts at 315( and ends at 45(, it is no good just switching them around (you’ll get the unwanted 270( of circle not the wanted 90(). The solution is simple. If the ending angle is too small, just add 360( to it (but remember that is really 2( again).

3. So how do you make the crescent?

You’ve got the two circles. All you have to do is work out their two points of intersection, and then work out the angles from the centres of the two circles to each of those intersection points.......

[pic]

The portion of the red circle from angle (1 to angle (2 must be drawn, followed by the portion of the blue circle from angle (1 to angle (2. The shape produced is shown in green above.

But the two arcs drawn must run smoothly together: the end of the red one must match the beginning of the blue one, so the blue arc must be drawn backwards, with the loop running down from (2 to (1.

4. And the calculations?

The red circle has radius r1 and centre (x1,y1); the blue circle has r2 and (x2,y2).

Calculate the distance between the two centres: d = sqrt((x2-x1)2+(y2-y1)2)

and the direction K = atan2(y1-y2, x2-x1). remember to add 2( if it’s negative.

Then A = acos((r12+d2-r22)/(2*r1*d))

The red starting angle (1 is K+A, and (2 is K-A.

Remember what I said about ensuring the ending angle (2 is greater than (1.

Now B = acos((r22+d2-r12)/(2*r2*d))

the blue angle (1 is K+(-B, and (2 is K+(+B.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download