|
|
|||||||
| About Us | Register | FAQ | Members List | Calendar | Mark Forums Read |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
-PM to advertise here-
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: In an average country of an average planet of the solar system of an average star of an average galaxy of an average cluster. Still not average...
Posts: 8,261
|
What am I doing wrong here? (C hurdle))
Well, I have to make a program that takes coordinates of a line (starting and ending point) and a rectangle (top left and bottom right) and tells us whether the line intersects the rectangle or not. I had the logic pretty much in my mind, there is no eror in the code but the execution is wrong. Can anybody help me out?
|
|
|
|
|
|
#2 (permalink) |
|
A1C
![]() ![]() Join Date: Jan 2004
Location: California
Posts: 230
|
What you need to do is show an example of how the output is wrong. Show the incorrect output and then show what it should be. Then ask for help.
Anyway, the program seems to work for me. Code:
Enter coordinates of (top left) (bottom right) (start of line) (end of line) respectively in x,y format: 0,5 5,0 4,4 4,6 Top Left = 0,5 Bottom Right = 5,0 Start of Line = 4,4 End of Line = 4,6 Press a key to continue The line intersects the rectangle at (4,5)
|
|
|
|
|
|
#3 (permalink) | |
|
-PM to advertise here-
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: In an average country of an average planet of the solar system of an average star of an average galaxy of an average cluster. Still not average...
Posts: 8,261
|
Quote:
|
|
|
|
|
|
|
#5 (permalink) | |
|
War Games coder
![]() ![]() ![]() ![]() ![]() Join Date: Apr 2001
Location: Florida
Posts: 1,918
|
Quote:
__________________
Primary CPU: Athlon 64 X2 4400+ Mobo: Biostar N4SLI-A9 RAM: 2G Crucial (DDR400) Video: eVGA GeForce 7900 GTX (512M) Audio: HDA X-Mystique HD(s): Maxtor 300G SATA2, Samsung 400G SATA2 OS(s): WinXP x64 Pro, Vista x32 Ultimate, Gentoo x64 Monitor(s): Primary - 19" Flat Panel (1280x1024) Secondary - 19" Flat Panel (1280x1024) Tertiary - Zenith 42" Plasma TV (1024x768 res) Many other machines... sig too short |
|
|
|
|
|
|
#6 (permalink) |
|
Emu author
![]() ![]() ![]() ![]() ![]() Join Date: Apr 2001
Location: Bloomington IN, USA
Posts: 1,061
|
Also, if you like // comments you should use them (I personally use them now in addition to classical C-style comments when I need multiline commenting)... it's virtually impossible to get stuck with a C compiler these days that doesn't support them. Besides, Ramsus is wrong, they're part of C99 ^_^
And I like using i, i2, i3, etc. for iterators, I think that makes me weird though :< - Exo |
|
|
|
|
|
#7 (permalink) |
|
-PM to advertise here-
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: In an average country of an average planet of the solar system of an average star of an average galaxy of an average cluster. Still not average...
Posts: 8,261
|
I guess I realize my problem somewhat. I am sending integers to the function to check if it intersects or not. It is also possible that the line intersects at a float point, isn't it?
|
|
|
|
|
|
#8 (permalink) | |
|
Emu author
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2003
Location: Victorville(Near Los Angeles or LA for those who are on the DL)
Posts: 839
|
Quote:
__________________
----------------- Emu Tinkerer and C++ Programmer |
|
|
|
|
|
|
#9 (permalink) | |
|
-PM to advertise here-
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: In an average country of an average planet of the solar system of an average star of an average galaxy of an average cluster. Still not average...
Posts: 8,261
|
Quote:
Consider this. I have started a loop and have sent points to the function to verify if it intersects. I sent for eg; 1,2,3,4,5,6,etc but what if the line intersects at 5.5? It will say that the line doesn't intersect. I hope you understand my point. |
|
|
|
|
|
|
#10 (permalink) |
|
Emu author
![]() ![]() ![]() ![]() ![]() Join Date: Apr 2001
Location: Bloomington IN, USA
Posts: 1,061
|
Your algorithm seems to leave something to be desired...
To find where two lines intersect you should solve a system of equations with two equations in the form y = ax + b. a and b are supplied constants that define the line, and x and y are the points you're solving for. So the function would be suppled a1, b1 and a2, b2 and ideally would return x and y somehow. Then you would see if that point is on the line segment to see if they intersected. But of course all of this isn't necessary to just see if they intersect or not. Your case is actually very simple because you're checking against a rectangle paralell to the x/y axes. I suggest having two functions.. one to see if the line interesects with a vertical line, and another to see if it intersects with a horizontal line. For a line segment to intersect with a horizontal line, the start y must be less than or equal to the y of the horizontal line, and the end y must be greater than or equal to the horizontal line. Either or both of the starting and ending x points must be within the starting/ending range of the horizontal line. Vertical is similar but you check to make sure the start/end x is within the x of the line and that one of the start/end y's is within the y range. - Exo |
|
|
|
|
|
#11 (permalink) |
|
-PM to advertise here-
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: In an average country of an average planet of the solar system of an average star of an average galaxy of an average cluster. Still not average...
Posts: 8,261
|
Yes, I made the program for a rectangle parallel to the x&y axes.
The main program divides the rectangle into four sides, sends each point of the side to the function which verifies an equation based on the two point formula of lines. (x-x1)/(x2-x1)=(y-y1)/(y2-y1) |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|