EZ Trigonometry for Graphic Positions X2 Y2

By Welopez, 5/3/06

I want to draw points on a circle or portion of a circle on a window for graphics. How can I calculate where those points should be?

When I was in school, I learned my sums and take-aways, times stuff and gozintas, but that college level math went over my head. I had a discussion with Artie Abacii, the Worble in charge of my math co-processor, and he said he could solve this problem in jig time. You see, we're thinking of this as a "circle problem," when it's actually a "triangle problem."

Consider this, we're standing at a point in the center of the screen, coordinates x and y, and we want to calculate the coordinates for a point on the circumference of a circle having a radius of n pixels. To mark that point, we need two coordinates, x2 and y2.

Just imagine you are at A, the x and y coordinates of your window, and you can calculate the x2 y2 coordinates for any other location on the window. Those worbles are pesky, but they will automatically adjust their calculations for locations up and right or down and left of your position.

Artie and Pablo got together and worked out this program to print points for the circumference of a circle.

      'Calculate X and Y coordinates to SET
      'by Welo and various Worbles, 050306

      NOMAINWIN
      WindowWidth=400
      WindowHeight=400

      OPEN "Hold Up A Flag" FOR GRAPHICS_nsb AS #a
      PRINT #a, "trapclose [quit]"
      PRINT #a, "DOWN"
      PRINT #a, "SIZE 5"
      PRINT #a, "SET 200 200" '< the center of our window

      FOR k=0 TO 360 STEP 6 'Begin at 0° and stop at 360°
      angle=k

      GOSUB [doMath] '< let Artie and assistants work out the math
      PRINT #a, "SET "; x2; " "; y2 '< Pablo's crew will mark the point
      GOSUB [pause]
      NEXT k

      WAIT
      [quit]
          CLOSE #a
          END

      [doMath]
          angle=(angle/57.29577951) 'Convert degrees to radians
          x2=(200+(sin(angle)*100)) 'Calculate x2 y2 coordinates from origin
          y2=(200-(cos(angle)*100)) 'for point on C with radius 100
      RETURN

      [pause]
      TIMER 25, [resume]
      WAIT
      [resume]
      TIMER 0
      RETURN

Of course, I don't understand much of that, so I had to ask my Worbles to explain it to me. Pablo is in the center of the screen. We tell Pablo we want to go 100 pixels in angle direction and mark the spot. Pablo is lazy and doesn't want to get out of his recliner, so he sends a message to Artii and asks him to calculate the position of that spot.

Artie converts the angle to a unit of measurement he understands (radians) and then starts calculating. He only understands two numbers, 1 and 0, so he lines up as many of his assistants as necessary and tells them, "All right you numb-skulls! If you are a one, hold up your left arm! If you are a zero, hold up your right arm!" Now Artii does the hard part, he sits in his recliner and counts iterations through the loop while his assistants get their exercise raising and lowering their arms.

From the center of the screen, x=200 and y=200, the location on the circumference for angle n will be x2=(SIN(n)*radius) + 200 pixels (because we started at x=200). To calculate y2, we use the cosine, y2=(COS(n)*radius) - 200 pixels (again, we started at y=200).

When Artii's assistants have finished all their exercises, he leans over to Pablo and says, "Send a gopher out to x2 y2 and tell him to put up a flag." Pablo yells at the next painter in the team and sends him running off to x2 y2. Pablo doesn't need to know the direction (degrees) and distance (pixels), because Artii has already calculated that for him. Pixels are a very large distance for tiny Worbles. Relatively speaking, I think each pixel is equivalent to about 123 kilometers for humans, but Worbles (like California drivers) don't pay any attention to posted speed limits, so they will paint your circle very quickly.

Now Artii and Pablo can sit back, sip lemonade, and wait for the next instruction from the FOR/NEXT loop. Hey, supervising is very hard work, you know?

Okay, the code above marks the seconds (or it could be minutes) on the face of a clock, beginning with 0 at the top of the clock, and stepping by 6 to 360 degrees. If you want a half circle, you could write:

      FOR k=90 to 270 '< no stepping, begin at right of window and continue to left

Artii will calculate the x2 y2 coordinates for every point and Pablo will send a gopher out to mark the point with a flag. A quarter circle, of course, is only 90 degrees, using any starting value and repeating until the ending value has been reached.

For a larger, or smaller, circle or arc, adjust the radius of the circle, which is actually the hypoteneuse of a triangle, in GOSUB [doMath] routine.

Watching all those painter Worbles running round and round, watching all the math Worbles waving their arms to represent binary calculations, has really tired me out. I think I'll take a nap for awhile. Hope you have fun working with your triangles... oops, I meant circles, of course