2
$\begingroup$

My question is a bit hard for me to express, so please bear with me. I never got far in trig, and haven't done much on the subject in years; trying to get back into it as it's a pretty major part of physics and I'm starting game development as a hobby.

Basically, I want to draw a pattern in the centre of a screen. That pattern will consist of a random amount of lines, all the same random length. These lines will all originate from one centre point, and will all be evenly spaced.

I'm working with a grid of pixels. Anyone familiar with graphical programming will know that most pixel grids start with the top left 0,0 in an x,y co-ordinate system. There are no negative points. Say my grid is 500x500. The centre pixel will be located at 250,250. 0,0 is the top-left pixel, 500,500 is the bottom right. All measurements in this question are in pixels.

Now say that this centre pixel, 250,250, is the exact centre of a circle. From now on let's call this centre point xy. The radius of the circle is r (assume it fits inside the grid).

There is another variable in play. Let's call it a. a is the amount of lines I want drawn from the centre of the circle to the edge of the circle, all evenly spaced (so r is the length of each line). I'm assuming that in order to do this, I have to do this: 360 / a, which gives me a degree (I'm totally making these variable names up) which I will somehow use to evenly space the lines. This leads to the question!

Given xy, r & a, how can I go about getting the x & y co-ordinates of the lines' end-points?

I hope this is clear enough! Please let me know if I need to clarify anything.

1 Answers 1

2

Each line extends from $(x,y)$ to $(x+r \cos \theta, y+r \sin \theta)$. You are right that if you want $a$ lines to fill up the circle, they should be spaced at $\frac {360}a$ degree intervals. So use $\theta = \frac {360n}a$ for $n=0,1,2,\ldots a-1$

  • 0
    The only part I don't understand is the `θ=360na` for `n=0,1,2,…a−1`. Can you clarify it? Again, I haven't done anything like this in years :)2012-05-28
  • 0
    @Djentleman: You want a total of $a$ lines. I was presuming you start with one at $\theta = 0$, so if you count there will be $a$ of them. The next one would be $\frac {360a}a=360$ and lie on top of the first. This equally spaces the lines in $\theta$. Is that what you want?2012-05-28
  • 0
    Ahh, I understand! And after some testing, the theta is registering properly :) One strange problem I'm getting is that the lines appear to be off, like the angle is slightly off. I'll leave this line of code here in case anyone can see the problem. `(x + (r * Math.cos(theta)), y + (r * Math.sin(theta)))`2012-05-28
  • 0
    @Djentleman: Are your pixels the same size in each direction? If your grid is $500 \times 500$ but the window is not square you need to correct for that.2012-05-28
  • 0
    All the pixels are the same size, the display is a perfect 500x500 square. The first line always ends up on theta=0 as it should, but the second seems to end up more on theta=145 rather than theta=180, and when there are more they appear splayed in equally strange ways.2012-05-28
  • 0
    @Djentleman: Are your trig functions expecting radians and you are giving them degrees? Try $\sin 45$ and see if you get $\frac {\sqrt 2}2$2012-05-28
  • 0
    Oh wow, how could I have missed that. Of course it was expecting radians! I just did `theta * PI / 180` and it fixed it. Thanks so much for your help!2012-05-28