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?
Offline
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...
Last edited by Hardmath123 (2012-03-26 07:33:01)
Offline
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...
I fixed your scratchblocks, there were too many brackets...
Offline
Hmm, for me it only seems to report yes on the line's endpoints.
Offline
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
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...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.
EDIT: When in doubt, round!
if <(round (((lineX2)-(pointX))/((lineX2)-(lineX1))))=(round (((lineY2)-(pointY))/((lineY2)-(lineY1))))> //...That should work.
Last edited by Hardmath123 (2012-03-26 09:13:32)
Offline
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
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 endHmm. That's strange.
Last edited by Hardmath123 (2012-03-26 10:26:24)
Offline
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 endUse another
<<> and <>>instead of nested ifs.
Offline
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 endUse 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
Last edited by legoscratch (2012-03-26 15:15:01)
Offline
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 endUse 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
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
Then you could just say
if <touching [lineColor]>
whatever
end
Anyway, my method worked, so... you're welcome, I guess.
Offline
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 endDoing 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)
Offline
Hardmath123 wrote:
Then you could just say
if <touching [lineColor]>
whatever
end
Anyway, my method worked, so... you're welcome, I guess.
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
Hardmath123 wrote:
Then you could just say
if <touching [lineColor]>
whatever
end
Anyway, my method worked, so... you're welcome, I guess.
I wanted to have different lines of the same color
... and I guess I was just sort of curious
Thanks anyway!
Offline