Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help! Slope problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Valce




PostPosted: Thu May 26, 2005 5:20 pm   Post subject: Help! Slope problem

How can I find the point of intersection of 2 line segments without using slopes?

Or if that's not possible... is there an easy way to code an exception for vertical lines when using?

Thanks!!
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Thu May 26, 2005 5:31 pm   Post subject: (No subject)

Assuming you know that the two lines segments touch, if the slope of one of your lines is vertical, you can solve for the point of intersection by subbing in the x value of the vertical line into the x in the equation for your second line.
In other words, you know the x value of the point of intersection is going to equal the x value of the vertical line. This is because any point on the vertical line has the same x value. So, you take that value and sub it into your second equation and solve.
Valce




PostPosted: Thu May 26, 2005 6:03 pm   Post subject: (No subject)

Thanks!


Is there an easy way to implement this? I have right now:
(I didn't include the beginning of this thing since it's built into a rather long shell... but basically a, b, a1, and b1 are endpoints and there are arrays of points/lines)

code:

                if b1.x not= a1.x and b.x not= a.x then
                    slope1 := (b1.y - a1.y) / (b1.x - a1.x)
                    intercept1 := a1.y - slope1 * a1.x
                    slope := (b.y - a.y) / (b.x - a.x)
                    intercept := a.y - slope * a.x
                    if slope not= slope1 then
                        pt (pointnum).x := (intercept - intercept1) / (slope1 - slope)
                        pt (pointnum).y := slope * (pt (pointnum).x) + intercept
                    else
                        parallel := true
                    end if

                else
                    if b1.x = a1.x and b.x not= a.x then
                        slope := (b.y - a.y) / (b.x - a.x)
                        intercept := a.y - slope * a.x
                        pt (pointnum).x := a1.x
                        pt (pointnum).y := slope * a1.x + intercept

                    elsif b.x = a.x and b1.x not= a1.x then
                        slope1 := (b1.y - a1.y) / (b1.x - a1.x)
                        intercept1 := a1.y - slope1 * a1.x
                        pt (pointnum).x := a.x
                        pt (pointnum).y := slope1 * a.x + intercept1

                    else
                        parallel := true
                    end if

                end if


                %display the point
                if parallel = true then
                    winID := Window.Open ("position:top;center,text:200;200")
                    put "Point of intersection not available"
                    put "Line number ", linenum, " and ", linenum1, " are parallel"
                    put "Press any key to continue"
                    ch := getchar
                    Window.Close (winID)

                else
                    selpt (0) := pointnum
                    DisplayPoint (pt (pointnum), dp, selcolour (0))
                    DisplayPointData (0, pointnum, pt (pointnum), cp)

                    if pt (pointnum).x < -310 or pt (pointnum).x > cp.left or pt (pointnum).y < -310 or pt (pointnum).y > maxy then
                        winID := Window.Open ("position:top;center,text:200;200")
                        put "Point outside of screen. Intersection located at (", pt (pointnum).x, ", ", pt (pointnum).y, ")"
                        put "Point stored as ", pointnum
                        put "Press any key to continue"
                        ch := getchar
                        Window.Close (winID)
                    end if
                end if

                parallel := false


And while it seems to work, it's also a tad long Sad


P.S. Sorry the code sort of runs down a line... The box is too small Sad
MysticVegeta




PostPosted: Fri May 27, 2005 3:35 pm   Post subject: (No subject)

Make 2 2d arrays: line1(x, y) and line2(x, y)
code:
for s : 1..upper(line1)
if line1(x, y) = line2(x, y)
intersect:= true
end if
end for


point of intersection can be found using this. Maybe lol
Cervantes




PostPosted: Fri May 27, 2005 3:44 pm   Post subject: (No subject)

That would require a ton of dots, and way too much processing time.
The idea is to find the point of intersection using math, not brute force. Math is always nicer than brute force.
Also, consider if your points were real numbers. You'd have to have such an insanely high number of points to actually get an answer. If you round things, you lose accuracy.
So math is more accurate and faster. Smile
Sorry Valce, can't look over your code right now. Hopefully someone else has the time. Wink
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: