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

#1 2012-03-26 06:42:34

legoscratch
Scratcher
Registered: 2010-06-18
Posts: 100+

Checking for a point on a line

I've been trying to find a way to see if a given point is on a line.
I know you can convert that line into an equation, but I think that would be slow and rather inaccurate with Scratch's rounding.
Any thoughts?


If you are having trouble with simple counting, use the following mnemonic device: 1 comes before 2 comes before 5,280 comes before 523,860,759 comes after the square root of -153 comes before π. This will make your previous counting problems seem like no big deal.

Offline

 

#2 2012-03-26 07:31:19

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Checking for a point on a line

Well, the math is actually quite easy!

if <((((lineX2)-(pointX)))/((lineX2)-(lineX1)))=((((lineY2)-(pointY)))/((lineY2)-(lineY1)))>
//...
Should work, but that's off the top of my head and I'm getting late for a class...  tongue

Last edited by Hardmath123 (2012-03-26 07:33:01)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#3 2012-03-26 07:46:19

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Checking for a point on a line

Hardmath123 wrote:

Well, the math is actually quite easy!

if <(((lineX2)-(pointX))/((lineX2)-(lineX1)))=(((lineY2)-(pointY))/((lineY2)-(lineY1)))>
//...
Should work, but that's off the top of my head and I'm getting late for a class...  tongue

I fixed your scratchblocks, there were too many brackets...

Offline

 

#4 2012-03-26 07:48:56

legoscratch
Scratcher
Registered: 2010-06-18
Posts: 100+

Re: Checking for a point on a line

Hmm, for me it only seems to report yes on the line's endpoints.


If you are having trouble with simple counting, use the following mnemonic device: 1 comes before 2 comes before 5,280 comes before 523,860,759 comes after the square root of -153 comes before π. This will make your previous counting problems seem like no big deal.

Offline

 

#5 2012-03-26 08:13:11

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Checking for a point on a line

legoscratch wrote:

Hmm, for me it only seems to report yes on the line's endpoints.

I was thinking that... Wait a minute, I'm trying to work it out...

Offline

 

#6 2012-03-26 09:09:58

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Checking for a point on a line

Splodgey wrote:

Hardmath123 wrote:

Well, the math is actually quite easy!

if <(((lineX2)-(pointX))/((lineX2)-(lineX1)))=(((lineY2)-(pointY))/((lineY2)-(lineY1)))>
//...
Should work, but that's off the top of my head and I'm getting late for a class...  tongue

I fixed your scratchblocks, there were too many brackets...

Thanks. As I said, it'll be something like that. You have to put Y2 as the higher point and X2 as the one more to the right.  wink

EDIT: When in doubt, round!  tongue

if <(round (((lineX2)-(pointX))/((lineX2)-(lineX1))))=(round (((lineY2)-(pointY))/((lineY2)-(lineY1))))>
//...
That should work.

Last edited by Hardmath123 (2012-03-26 09:13:32)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#7 2012-03-26 09:24:52

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Checking for a point on a line

I'm not sure but this might work:

<<<[Line X start] < [point x]> and <[Line X end] > [point x]>> and <<[Line Y start] < [point y]> and <[Line Y end] > [point y]>>> //huh I thought I trying adding a >

Last edited by Splodgey (2012-03-27 04:04:14)

Offline

 

#8 2012-03-26 10:24:02

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Checking for a point on a line

if <<<[Line X start] < [point x]> and <[Line X end] > [point x]>> and <<[Line Y start] <[point y]> and <[Line Y end] > [point y]>>>
bleh
end
Hmm. That's strange.

Last edited by Hardmath123 (2012-03-26 10:26:24)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#9 2012-03-26 10:36:34

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Checking for a point on a line

Hmm...

if <<[Line X start] < [point x]> and <[Line X end] > [point x]>>
if <<[Line Y start] < [point y]> and <[Line Y end] > [point y]>>
well that worked
end
end
Use another
<<> and <>>
instead of nested ifs.

Offline

 

#10 2012-03-26 15:01:25

legoscratch
Scratcher
Registered: 2010-06-18
Posts: 100+

Re: Checking for a point on a line

Splodgey wrote:

Hmm...

if <<[Line X start] < [point x]> and <[Line X end] > [point x]>>
if <<[Line Y start] < [point y]> and <[Line Y end] > [point y]>>
well that worked
end
end
Use another
<<> and <>>
instead of nested ifs.

But that will report the bounding box, not the line itself.

Edit:
Just tested Hardmath's method and it works great!
Had to round to the nearest tenth, though.
Then to make it work with only the segment and not its entire line, I made sure it was inside the line's bounding box.

Edit 2:

<<<[Line X start] < [point x]> and <[Line X end] > [point x]>> and <<[Line Y start] < [point y]> and <[Line Y end] > [point y]>>> //huh why is this cutting off?
Needed one more > at the end  wink

Last edited by legoscratch (2012-03-26 15:15:01)


If you are having trouble with simple counting, use the following mnemonic device: 1 comes before 2 comes before 5,280 comes before 523,860,759 comes after the square root of -153 comes before π. This will make your previous counting problems seem like no big deal.

Offline

 

#11 2012-03-27 04:03:25

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Checking for a point on a line

legoscratch wrote:

Splodgey wrote:

Hmm...

if <<[Line X start] < [point x]> and <[Line X end] > [point x]>>
if <<[Line Y start] < [point y]> and <[Line Y end] > [point y]>>
well that worked
end
end
Use another
<<> and <>>
instead of nested ifs.

But that will report the bounding box, not the line itself.

Edit:
Just tested Hardmath's method and it works great!
Had to round to the nearest tenth, though.
Then to make it work with only the segment and not its entire line, I made sure it was inside the line's bounding box.

Edit 2:

<<<[Line X start] < [point x]> and <[Line X end] > [point x]>> and <<[Line Y start] < [point y]> and <[Line Y end] > [point y]>>> //huh why is this cutting off?
Needed one more > at the end  wink

It will work for both boxes and lines. A line is a box but just a few pixels wide/high...



EDIT: Ah, Diagonal lines!

<<> and <touching color [line colour]?>>
Really, Really bad way to fix it but it's all I can think of right now...

Last edited by Splodgey (2012-03-27 04:07:12)

Offline

 

#12 2012-03-29 09:53:57

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Checking for a point on a line

Then you could just say
if <touching [lineColor]>
whatever
end

Anyway, my method worked, so... you're welcome, I guess.  tongue


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#13 2012-03-29 11:05:19

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Checking for a point on a line

You could use the actual equation for the line by doing this:

set [m v] to (((Y2) - (Y1)) / ((X2) - (X1))) // calculate and save slope
set [b v] to ((Y2) - ((m) * (X2))) // derived from y=mx+b to b=y-mx
if <[3] > ([abs v] of ((((m) * (xtest)) + (b)) - (ytest)))>
  action here
end
Doing these calculations won't slow down your script...not noticeably anyway.  Also, this way you can give a range that your y value can be in in order to be on the line.  Right now the range is 6 surrounding the line but you can also change it to just being less than the line, closer to the line, above the line, etc.

Last edited by AtomicBawm3 (2012-03-29 11:08:28)


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#14 2012-03-29 11:09:32

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Checking for a point on a line

Hardmath123 wrote:

Then you could just say
if <touching [lineColor]>
whatever
end

Anyway, my method worked, so... you're welcome, I guess.  tongue

But if there where other things with the same colour...

Your's worked but I thought it didn't at first...

Last edited by Splodgey (2012-03-30 08:54:02)

Offline

 

#15 2012-03-30 07:11:58

legoscratch
Scratcher
Registered: 2010-06-18
Posts: 100+

Re: Checking for a point on a line

Hardmath123 wrote:

Then you could just say
if <touching [lineColor]>
whatever
end

Anyway, my method worked, so... you're welcome, I guess.  tongue

I wanted to have different lines of the same color
... and I guess I was just sort of curious
Thanks anyway!


If you are having trouble with simple counting, use the following mnemonic device: 1 comes before 2 comes before 5,280 comes before 523,860,759 comes after the square root of -153 comes before π. This will make your previous counting problems seem like no big deal.

Offline

 

Board footer