This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2009-11-13 19:45:04

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Algortithim

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

 

#2 2009-11-13 20:30:51

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Algortithim

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)


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#3 2009-11-13 20:35:12

illusionist
Retired Community Moderator
Registered: 2008-07-02
Posts: 1000+

Re: Algortithim

[blocks]<distance to[ [/blocks]

Is this what you mean?  neutral


http://i.imgur.com/8LX1NrV.png

Offline

 

#4 2009-11-13 20:50:12

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Algortithim

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  smile

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

 

#5 2009-11-13 21:18:54

samurai768
Scratcher
Registered: 2009-07-21
Posts: 1000+

Re: Algortithim

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

 

#6 2009-11-13 21:33:16

Greatdane
Scratcher
Registered: 2007-06-05
Posts: 1000+

Re: Algortithim

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

http://www.imagebeast.net/images/79gc1aj33x3bguiovczj.gif


The future belongs to those who believe in the beauty of their dreams.
        ~ Eleanor Roosevelt

Offline

 

#7 2009-11-14 10:48:54

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: Algortithim

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  smile

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

 

#8 2009-11-14 22:33:44

Greatdane
Scratcher
Registered: 2007-06-05
Posts: 1000+

Re: Algortithim

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.  smile  Why do you need that anyway?


The future belongs to those who believe in the beauty of their dreams.
        ~ Eleanor Roosevelt

Offline

 

#9 2009-11-14 22:38:32

Greatdane
Scratcher
Registered: 2007-06-05
Posts: 1000+

Re: Algortithim

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')


The future belongs to those who believe in the beauty of their dreams.
        ~ Eleanor Roosevelt

Offline

 

#10 2009-11-15 08:40:17

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: Algortithim

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.  smile  Why do you need that anyway?

Your talking about distance. It is useful, but I need coordinates.

Offline

 

#11 2009-11-15 08:42:09

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: Algortithim

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

 

Board footer