I'm looking for an algorithim that would allow me to calculate all of the x and y points inbetween 2. Lets say I have a 2 points like here:
___________________________________
| |
| (a) . |
| |
| |
| |
| (b) . |
| |
| |
|__________________________________|
I want to calcuate all of the other points between them and put them in a list. Thanks in advance for any help!
Offline
I think you need to make two lists an x and y then get a sprite to go to one of the sprites (you can make it hide). Then get it to point in the direction of the other sprite and make it move 1 step and a each step record it's x and y position on a new line of the x and y list. When it's x and y position is grater then the other sprites delete the last line on each list and you should have every x and y value in between each sprite.
Note: doing it this way will have large demical places so I would recomend geting the absolute values of the x and y.
Last edited by what-the (2009-11-13 20:31:23)
My site Offline
That's a cool problem. I assume you want to compute all the _pixels_ between the two points, rather than all the _points_ , since there are an infinite amount
Here's an algorithm that will work:
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
but also see:
http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
Offline
Heres an idea, download the mouse recorder, and draw a line between the two points. Take the list that it uses, and put it in your project. Thats how I would do it XD
Offline
Look at this:
| A. |
| | \ |
| ---- \ b.|
Do you see something vaguely looking like a triangle?
Now take the x and y of point b:
(x=1,y=5)
and the x/y of point a: | \
(x=5, y=1) | \
And find the length of the first side: --------------b.
It's the x of a minus the x of b:
5-1 = 4
Then take the y of point b:
5
then take the y of point a:
1
and subtract
5-1 = 4.
So now what does your triangle look like?
A.
| \
| \
------- B.
The length of green is C.
The length of red is 4.
The length of orange is 4.
So let's plug this into the Pythagorean theorem:
a2 + b2 = c2
4(4) + 4(4) = c2
16(2) = c2
31 = c2
Then take the square root, and you get C is equal to about 5.6.
That's it! That's the distance between A and B!
In Scratchish
Offline
chalkmarrow wrote:
That's a cool problem. I assume you want to compute all the _pixels_ between the two points, rather than all the _points_ , since there are an infinite amount
![]()
Here's an algorithm that will work:
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
but also see:
http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
Chalkmarrow's was probably the most accurate, but almost all of the code is written in different programming languages, and it is hard to translate into Scratch. Let me explain one more time what I am trying to do.
I want to calculate all of the coordinates between point A and point B. I then want to put them in a list. I do NOT want the distance.
I appreciate all the other suggestions about using a mouse recorder or using actual movement, but I really need efficiency, so I would prefer an algorithm.
Last edited by shadow_7283 (2009-11-14 10:50:04)
Offline
I know that the Pythagorean theorem is usually the best way to do so, but if you want to use any other things, fine, go ahead.
Why do you need that anyway?
Offline
I think that you can, after finding the distance, calculate all the points by finding the degree of the line, but that's probably within the realm of advanced mathematics(eg 'trigometry')
Offline
Greatdane wrote:
I know that the Pythagorean theorem is usually the best way to do so, but if you want to use any other things, fine, go ahead.
Why do you need that anyway?
Your talking about distance. It is useful, but I need coordinates.
Offline
Greatdane wrote:
I think that you can, after finding the distance, calculate all the points by finding the degree of the line, but that's probably within the realm of advanced mathematics(eg 'trigometry')
That's what I'm asking for! Bring it on!
Offline