GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jan-2009, 23:35
mandar_999 mandar_999 is offline
New Member
 
Join Date: Jan 2009
Posts: 9
mandar_999 is an unknown quantity at this point
Arrow

How to solve this problem


Hello

i have to solve a mathematical problem
can any one tell me how shud i go for it ???????
i have coordinates of 2 points
i also have coordinates of 4 points which forms a square.
i have to write a program to find out whether the line formed by given 2 points intersects the
square formed by rest 4 points.
can anyone give me any idea to solve it ??????????

Thank you
  #2  
Old 05-Jan-2009, 02:50
baccardi baccardi is offline
Junior Member
 
Join Date: Mar 2006
Posts: 48
baccardi is on a distinguished road

Re: How to solve this problem


if any coordinates of the square are in the boundaries of the coordinates of the line then the line intersects that square
  #3  
Old 05-Jan-2009, 04:11
mandar_999 mandar_999 is offline
New Member
 
Join Date: Jan 2009
Posts: 9
mandar_999 is an unknown quantity at this point
Exclamation

Re: How to solve this problem


if any coordinates of the square are in the boundaries of the coordinates of the line then that doesn't mean always that line intersects that square.This case is not applicable always.

can u elaborate a bit on this.
  #4  
Old 05-Jan-2009, 09:08
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: How to solve this problem


If the line segment intersects the square, it must intersect one of the two line segments between opposite corners of the square. Thus if v1, v2, v3, and v4 are the vertices of the square (in order, going around the square), then you need to check if your line segment intersects either the line segment v1v3 or the line segment v2v4.
__________________
www.blake-foster.com
  #5  
Old 05-Jan-2009, 10:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to solve this problem


Quote:
Originally Posted by Blake
If the line segment intersects the square, it must intersect one of the two line segments between opposite corners of the square....


Hmmm...
I don't believe that is adequate to determine intersection of a line segment with a square.

I drew a square and its diagonals and scribbled a few example lines that illustrate my point, and I attached the drawing to this post

My observations:

1. I don't think that intersection with a diagonal is a necessary condition for intersection with a side of the square.

2. I don't think that inequalities involving coordinates of the end points of the line and coordinates of the vertices will tell the story either. (Unless, that is, the sides of the square are aligned with the coordinate system, in which case it's pretty easy to do it this way.)


Without getting into some interesting aspects of computational geometry (convex hulls, dot products, etc.), for the general (non-axis-aligned square) I think one could start by determining whether the given line segment intersects at least one of the sides of the square.

I have a question: What is the definition of a "line segment intersecting with a square"? What if the line segment is contained totally inside the square? Does that count? Or does "intersection of a line segment with a square" imply intersection of the given line segment with one or more of the line segments that make up the sides of the square? (I assume that two line segments are said to intersect if they have at least one point in common.)

A final question (maybe already answered by the answer to the previous question): Is there a difference between a "line segment intersecting a square" and a "line segment colliding with a square"? I think that game programmers are more likely to use the "collision" terminology.

Regards,

Dave
Attached Images
File Type: pdf Intersections.pdf (3.1 KB, 18 views)
Last edited by davekw7x : 05-Jan-2009 at 11:17.
  #6  
Old 05-Jan-2009, 11:23
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: How to solve this problem


Hmm, good point, it always help to draw a picture. I was only thinking of the case where both endpoints are outside of the square.

Try checking intersections with the diagonals, and also checking if one endpoint is inside the square.
__________________
www.blake-foster.com
  #7  
Old 06-Jan-2009, 00:10
mandar_999 mandar_999 is offline
New Member
 
Join Date: Jan 2009
Posts: 9
mandar_999 is an unknown quantity at this point

Re: How to solve this problem


hey thanks for considering all different combination for this problem
well i want to clear here that lenght of the line is more than the diagonal of the square
i mean in my case line will be always more than the square
as u suggested even i was thinking like determining whether the given line segment intersects at least one of the sides of the square or not.
but i am not getting how to find out whether a line intersects a line segment
i mean i know how to find out a point where 2 lines intersects each other or not, but in this case i have to find out whether a line intersect a segment or not
is thr any such formula for it
or can u tell me how should i go for it.
plz elaborate a bit.


ya and line colliding with square is also considered as intersection in this case.
even if line just passing through any of the vertices can also be considered here as an intersection.
  #8  
Old 06-Jan-2009, 05:06
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: How to solve this problem


Here's an algorithm (in pseudocode) straight out of my algorithms text:

Code:
segmentsIntersect(p1, p2, q1, q2) d1 = direction(q1, q2, p1) d2 = direction(q1, q2, p2) d3 = direction(p1, p2, q1) d4 = direction(p1, p2, q2) if (d1*d2 < 0 || d3*d4 < 0) return true else if (d1 == 0 && onSegment(q1, q2, p1) return true else if (d2 == 0 && onSegment(q1, q2, p2) return true else if (d3 == 0 && onSegment(p1, p2, q1) return true else if (d4 == 0 && onSegment(p1, p2, q2) return true else return false direction(p1, p2, p3) q1 = p3 - p1 q2 = p2 - p1 return q1.x*q2.y - q2.x*q1.y < 0 onSegment(p1, p2, p3) return min(p1.x, p2.x) <= p3.x <= max(p1.x, p2.x) && min(p1.y, p2.y) <= p3.y <= max(p1.y, p2.y)

It returns true if and only if the segment p1p2 intersects the segment q1q2.

If you use floating point numbers, you should use a threshold when testing for equality. In other words, rather than testing if a == b, test if a - epsilon < b < a + epsilon, where epsilon is your threshold.
__________________
www.blake-foster.com
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Write a calculator program that lets users enter a simple mathematic operation kcp88 C Programming Language 21 09-Sep-2008 22:15
I have a problem to solve summation in c++ logieen C++ Forum 4 22-May-2007 07:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:51.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.