Author Topic: Moving (X,Y) in a Circular Path  (Read 4701 times)

0 Members and 1 Guest are viewing this topic.

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Moving (X,Y) in a Circular Path
« on: January 06, 2019, 04:06:06 pm »
Using the following code, I get a nice, perfect circle drawn (the real goal is to move points in a circular path, of course):

Code: QB64: [Select]
  1.  
  2. CX = 320: CY = 240 'Centre of circle
  3. Rad = 120 'Radius
  4.  
  5. 'FOR Ang = 0 TO 359
  6. FOR Ang = 0 TO 6.28 STEP 1 / 360
  7.     _LIMIT 240 ':CLS
  8.  
  9.     X = CX + COS(Ang) * Rad
  10.     Y = CY + SIN(Ang) * Rad
  11.  
  12.     PSET (X, Y)
  13.     LOCATE 11, 10: PRINT USING "Angle: ###.###"; Ang:
  14.  
  15.  

But my problem is that, in my game, my angles run from 0 to 359, or similar.
How can I adapt the code to work with the very different numbers?  Thanks!
(NOTE: The above code won't actually be used as-is in my prog.;  I'm merely trying to gain an understanding of this here stuff.)

Joe
« Last Edit: January 06, 2019, 04:08:23 pm by SirCrow »
I may not always finish what I've started....

Marked as best answer by SirCrow on January 06, 2019, 03:55:20 pm

FellippeHeitor

  • Guest
Re: Moving (X,Y) in a Circular Path
« Reply #1 on: January 06, 2019, 04:10:32 pm »
COS() and SIN() expect radians instead of angles, so you can use _D2R to convert from Degrees to Radians:

Code: QB64: [Select]
  1.  
  2. CX = 320: CY = 240 'Centre of circle
  3. Rad = 120 'Radius
  4.  
  5. 'FOR Ang = 0 TO 359
  6. FOR Ang = 0 TO 360 STEP .1
  7.     _LIMIT 240 ':CLS
  8.  
  9.     X = CX + COS(_D2R(Ang)) * Rad
  10.     Y = CY + SIN(_D2R(Ang)) * Rad
  11.  
  12.     PSET (X, Y)
  13.     LOCATE 11, 10: PRINT USING "Angle: ###.###"; Ang:
  14.  
  15.  

The added STEP .1 is so that the circle doesn't have leak areas.

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #2 on: January 06, 2019, 05:16:07 pm »
COS() and SIN() expect radians instead of angles, so you can use _D2R to convert from Degrees to Radians:
. . .

Thanks, Fellippe.  I did not know about _D2R and the others.  I was thinking about dividing by 57.3 or something like that, but I might've known there was already something quick and easy available.
I may not always finish what I've started....

FellippeHeitor

  • Guest
Re: Moving (X,Y) in a Circular Path
« Reply #3 on: January 06, 2019, 05:36:51 pm »
All it does is value * 0.01745329251994329576923690768489

Offline codeguy

  • Forum Regular
  • Posts: 174
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #4 on: January 07, 2019, 04:07:12 am »
There's a better way to make a leakproof circle than using a constant to determine the number of steps. 2pi*radius gives you the minimum number of steps necessary to generate a circle that does not leak when painted or flood-filled, although there are faster methods than both of these to do this.

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #5 on: January 08, 2019, 12:47:21 pm »
There's a better way to make a leakproof circle than using a constant to determine the number of steps. 2pi*radius gives you the minimum number of steps necessary to generate a circle that does not leak when painted or flood-filled, although there are faster methods than both of these to do this.

That sounds as if it would work when radians are used;  I'm working with degrees.

In any case, my real objective is not to draw a leakproof circle, but simply to move objects in a circular path around another object.
For years and years I'd wondered how others did it, and now I'm finally starting to understand it myself.
I may not always finish what I've started....

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #6 on: January 08, 2019, 04:35:04 pm »
To move in spiral around a point do the same as circle only change the radius with the angle for the x, y points. Spiral out by adding to radius and spiral in by subtracting from radius.

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #7 on: January 08, 2019, 05:12:13 pm »
To move in spiral around a point do the same as circle only change the radius with the angle for the x, y points. Spiral out by adding to radius and spiral in by subtracting from radius.

Being curious, I had already tried it, but I got unpredictable results.  I'll be playing around with that in the future.  Thanks.
I may not always finish what I've started....

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #8 on: January 08, 2019, 06:22:37 pm »
Code: QB64: [Select]
  1.  
  2. CX = 320: CY = 240 'Centre of circle
  3. Rad = 1 'Radius
  4.  
  5. 'FOR Ang = 0 TO 359
  6. FOR Ang = 0 TO 360 * 8 'spin around 8 times
  7.     _LIMIT 200 ':CLS
  8.     Rad = Rad + .1
  9.     X = CX + COS(_D2R(Ang)) * Rad
  10.     Y = CY + SIN(_D2R(Ang)) * Rad
  11.  
  12.     PSET (X, Y)
  13.     LOCATE 11, 10: PRINT USING "Angle: ###,###.###"; Ang:
  14.  
  15.  
  16.  
« Last Edit: January 08, 2019, 06:23:58 pm by bplus »

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #9 on: January 08, 2019, 09:49:43 pm »

I don't understand why it's OK for Ang to to past 360, but the result is pretty cool.
I may not always finish what I've started....

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Moving (X,Y) in a Circular Path
« Reply #10 on: January 08, 2019, 10:02:19 pm »

I don't understand why it's OK for Ang to to past 360, but the result is pretty cool.

Spin 360 degrees... You’ve made a complete circle.

Now spin 720 degrees.   You’ve rotated in place twice, making 2 circles...

1080 degrees....  3 complete rotations...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #11 on: January 08, 2019, 10:20:47 pm »

I don't understand why it's OK for Ang to to past 360, but the result is pretty cool.

Spin 360 degrees... You’ve made a complete circle.

Now spin 720 degrees.   You’ve rotated in place twice, making 2 circles...

1080 degrees....  3 complete rotations...

To go further will require a new keyword... _CONGRESS

It is pretty amazing how powerful this stuff can be in gaming / graphics. I just ran Mark's "Boing" code today and frankly, I did not expect such good quality with so few lines of code. Good luck Joe with your game, too.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Moving (X,Y) in a Circular Path
« Reply #12 on: January 08, 2019, 10:56:58 pm »

It is pretty amazing how powerful this stuff can be in gaming / graphics. I just ran Mark's "Boing" code today and frankly, I did not expect such good quality with so few lines of code. Good luck Joe with your game, too.

Pete

Thanks very much.
I may not always finish what I've started....