QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: SirCrow on January 06, 2019, 04:06:06 pm

Title: Moving (X,Y) in a Circular Path
Post by: SirCrow 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
Title: Re: Moving (X,Y) in a Circular Path
Post by: FellippeHeitor 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.
Title: Re: Moving (X,Y) in a Circular Path
Post by: SirCrow 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.
Title: Re: Moving (X,Y) in a Circular Path
Post by: FellippeHeitor on January 06, 2019, 05:36:51 pm
All it does is value * 0.01745329251994329576923690768489
Title: Re: Moving (X,Y) in a Circular Path
Post by: codeguy 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.
Title: Re: Moving (X,Y) in a Circular Path
Post by: SirCrow 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.
Title: Re: Moving (X,Y) in a Circular Path
Post by: bplus 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.
Title: Re: Moving (X,Y) in a Circular Path
Post by: SirCrow 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.
Title: Re: Moving (X,Y) in a Circular Path
Post by: bplus 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.  
Title: Re: Moving (X,Y) in a Circular Path
Post by: SirCrow 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.
Title: Re: Moving (X,Y) in a Circular Path
Post by: SMcNeill 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...
Title: Re: Moving (X,Y) in a Circular Path
Post by: Pete 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
Title: Re: Moving (X,Y) in a Circular Path
Post by: SirCrow 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.