| 08-23-2004, 06:06 PM | #1 |
ok i am trying to create a function that takes 2 lines each defined by 2 points and returns where they intersect, but i keep on getting some weird location back, help =( Code:
function intersection takes location line1start, location line1end, location line2start, location line2end returns location local real ax1 local real ay1 local real ax2 local real ay2 local real bx1 local real by1 local real bx2 local real by2 local real m1 local real m2 local real b1 local real b2 local real intersectx local real intersecty set ax1 = GetLocationX(line1start) set ay1 = GetLocationY(line1start) set ax2 = GetLocationX(line1end) set ay2 = GetLocationY(line1end) set bx1 = GetLocationX(line2start) set by1 = GetLocationY(line2start) set bx2 = GetLocationX(line2end) set by2 = GetLocationY(line2end) set m1 = (ay1-ay2)/(ax1-ax2) set m2 = (by1-by2)/(bx1-bx2) set b1 = -1*((m2*ax1)-ay1) set b2 = -1*((m2*bx1)-by1) set intersectx = (b2-b1)/(m1-m2) set intersecty = (m1*intersectx)+b1 return Location(intersectx,intersecty) endfunction the reason that there are so many variables is to keep track of whats going on, later i am going to simplify this but right now i just want it to work. =( |
| 08-23-2004, 07:49 PM | #2 |
Code:
function intersection takes location line1start, location line1end, location line2start, location line2end returns location local real ax1 local real ay1 local real ax2 local real ay2 local real bx1 local real by1 local real bx2 local real by2 local real m1 local real m2 local real b1 local real b2 local real intersectx local real intersecty set ax1 = GetLocationX(line1start) set ay1 = GetLocationY(line1start) set ax2 = GetLocationX(line1end) set ay2 = GetLocationY(line1end) set bx1 = GetLocationX(line2start) set by1 = GetLocationY(line2start) set bx2 = GetLocationX(line2end) set by2 = GetLocationY(line2end) set m1 = (ay2 - ay1) / (ax2 - ax1) set m2 = (by2 - by1) / (bx2 - bx1) set b1 = ay1 - m1 * ax1 set b2 = by1 - m2 * bx1 set intersectx = (b2 - b1) / (m1 - m2) set intersecty = m1 * intersectx +b1 return Location(intersectx,intersecty) endfunction I believe that should fix it. Edit: Hmmm, you might run into trouble with verticle lines. |
| 08-23-2004, 09:37 PM | #3 |
SWEEET IT WORKS can you just explain why you made that change? |
| 08-23-2004, 09:44 PM | #4 |
You might also considering adding onto that function. Things to consider: - parallel lines - vertical lines - is the intersection within the 'region' defined by the locations - is the intersection within the playable map bounds |
| 08-23-2004, 10:02 PM | #5 |
You just had your equations mixed up. y = mx + b m1 = (y2 - y1) / (x2 - x1) m2 = (y4 - y3) / (x4 - x3) b1 = y1 - m1 * x1 b2 = y3 - m2 * x3 ix = (b2 - b1) / (m1 - m2) iy = m1 * ix + b1 |
| 08-23-2004, 11:02 PM | #6 |
BDSM - yeah i plan to add those along with cleaning the function up but what do you mean by vertical lines? Stonewal - im still confuzed could you just explain the b= forumals? and Thanks for everything =) |
| 08-23-2004, 11:34 PM | #7 |
In the formula 'y = mx + b', 'b' is the 'y intercept'. To find out what 'b' is you just rearrange the formula a little. y = mx + b Minus 'mx' from both sides. y - mx = b Flip it around. b = y - mx Here's your formula for 'b'. |
| 08-24-2004, 12:26 AM | #8 |
oh damnit thats so simple i was being difficult and trying to solve it this way y = mx+b -b -b ______________ -b+ y = mx - y -y ______________ -b = mx - y and then multiply both sides by -1, but i guess that does work ;) thanks man! |
| 08-24-2004, 03:35 AM | #9 |
Vertical lines being where m = number /zero. If you have 2 vertical lines, (straight up), obviously they are parallel. However, if you have one, you automatically know the x intercept for your 2 lines, x-intercept being the x of the vertical line. |
