Emuforums.com

Go Back   Emuforums.com > General Discussion > Web development / Programming
About Us Register FAQ Members List Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old April 6th, 2004   #1 (permalink)
-PM to advertise here-
 
Syed Fawad's Avatar
 
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?
Attached Files
File Type: zip NEW.zip (665 Bytes, 39 views)
__________________

Asus K8N (nForce3)
AMD64 3000+ (754)
768MB DDR 400MHz (PC 3200)
nVidia Geforce 6600GT 128MB (AGP)

Aquamark3 - 53,000

"Good friends mustn't always be together. It is the feeling of oneness when distant that proves a lasting friendship"

Syed Fawad is offline   Reply With Quote
Old April 6th, 2004   #2 (permalink)
A1C
 
Ramsus K's Avatar
 
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)
Some notes about C programming:
  • Never use void main(). It should be int main(). void main() goes against the standard and can result in undefined behavior.
  • // is a C++ style comment. Don't use them in C.
  • It's generally accepted style these days to put a space between the #include and <. e.g., #include <stdio.h> instead of #include<stdio.h>
  • conio.h is not part of the standard C library. Avoid it when unnecessary, since it's Windows/DOS specific. I have to edit out that code to compile it.
  • exit() is declared in <stdlib.h>. Include it when you want to use exit.
  • The interface is a horrible hack using scanf. Take each integer ONE AT A TIME. It's much cleaner that way, and you avoid problems with the user making a minor typo in his formatting. It's even better to write your own input function for integer points using fgets and atol with some error checking code. You can add abstraction by using structures to represent the points.
  • Use some easier to read variable names or use comments. Don't use a or b as iterator variables. Start with i and proceed onward (e.g. i, j, k, l, m).
  • Keep your code less than 80 characters wide.
  • Avoid using exit. Also, it should be exit(0); exit(EXIT_SUCCESS); or exit(EXIT_FAILURE);. Other values can have undefined behavior. The same applies to returning from int main(). return 0; return EXIT_SUCCESS; or return EXIT_FAILURE; but nothing else. #include <stdlib.h> if you want to use the EXIT_SUCCESS or EXIT_FAILURE macros.
Ramsus K is offline   Reply With Quote
Old April 6th, 2004   #3 (permalink)
-PM to advertise here-
 
Syed Fawad's Avatar
 
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:
Originally Posted by Ramsus K
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)
Some notes about C programming:
  • Never use void main(). It should be int main(). void main() goes against the standard and can result in undefined behavior.
  • // is a C++ style comment. Don't use them in C.
  • It's generally accepted style these days to put a space between the #include and <. e.g., #include <stdio.h> instead of #include<stdio.h>
  • conio.h is not part of the standard C library. Avoid it when unnecessary, since it's Windows/DOS specific. I have to edit out that code to compile it.
  • exit() is declared in <stdlib.h>. Include it when you want to use exit.
  • The interface is a horrible hack using scanf. Take each integer ONE AT A TIME. It's much cleaner that way, and you avoid problems with the user making a minor typo in his formatting. It's even better to write your own input function for integer points using fgets and atol with some error checking code. You can add abstraction by using structures to represent the points.
  • Use some easier to read variable names or use comments. Don't use a or b as iterator variables. Start with i and proceed onward (e.g. i, j, k, l, m).
  • Keep your code less than 80 characters wide.
  • Avoid using exit. Also, it should be exit(0); exit(EXIT_SUCCESS); or exit(EXIT_FAILURE);. Other values can have undefined behavior. The same applies to returning from int main(). return 0; return EXIT_SUCCESS; or return EXIT_FAILURE; but nothing else. #include <stdlib.h> if you want to use the EXIT_SUCCESS or EXIT_FAILURE macros.
Thanks for those guidlines. I'll take care next time. As for the program running fine for you, well would you please test some more coordinates to see if it is running ok?
__________________

Asus K8N (nForce3)
AMD64 3000+ (754)
768MB DDR 400MHz (PC 3200)
nVidia Geforce 6600GT 128MB (AGP)

Aquamark3 - 53,000

"Good friends mustn't always be together. It is the feeling of oneness when distant that proves a lasting friendship"

Syed Fawad is offline   Reply With Quote
Old April 6th, 2004   #4 (permalink)
A1C
 
Ramsus K's Avatar
 
Join Date: Jan 2004
Location: California
Posts: 230
Okay, I'll test it some more later if I have time. If I can, I'll write a script to test it.
Ramsus K is offline   Reply With Quote
Old April 12th, 2004   #5 (permalink)
War Games coder
 
KillerShots's Avatar
 
Join Date: Apr 2001
Location: Florida
Posts: 1,918
Quote:
Originally Posted by Ramsus K
Avoid using exit. Also, it should be exit(0); exit(EXIT_SUCCESS); or exit(EXIT_FAILURE);. Other values can have undefined behavior. The same applies to returning from int main(). return 0; return EXIT_SUCCESS; or return EXIT_FAILURE; but nothing else. #include <stdlib.h> if you want to use the EXIT_SUCCESS or EXIT_FAILURE macros.
I disagree with this. When writing scripts which interact with your programs, you can switch on what error conditions occurred after execution - for example, failed due to file IO could return 1, failed due to bad initialization could return 2, etc. They are called error codes for a reason. The only standard put in place is that a 0 is successful execution, and 1 normally means there was a problem.
__________________
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
KillerShots is offline   Reply With Quote
Old April 13th, 2004   #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
Exophase is offline   Reply With Quote
Old April 14th, 2004   #7 (permalink)
-PM to advertise here-
 
Syed Fawad's Avatar
 
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?
__________________

Asus K8N (nForce3)
AMD64 3000+ (754)
768MB DDR 400MHz (PC 3200)
nVidia Geforce 6600GT 128MB (AGP)

Aquamark3 - 53,000

"Good friends mustn't always be together. It is the feeling of oneness when distant that proves a lasting friendship"

Syed Fawad is offline   Reply With Quote
Old April 14th, 2004   #8 (permalink)
Emu author
 
zenogais's Avatar
 
Join Date: Aug 2003
Location: Victorville(Near Los Angeles or LA for those who are on the DL)
Posts: 839
Quote:
Originally Posted by Syed Fawad
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?
That shouldn't matter, if the result of an integer operation is a floating point then it is just rounded.
__________________
-----------------
Emu Tinkerer and C++ Programmer
zenogais is offline   Reply With Quote
Old April 15th, 2004   #9 (permalink)
-PM to advertise here-
 
Syed Fawad's Avatar
 
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:
Originally Posted by zenogais
That shouldn't matter, if the result of an integer operation is a floating point then it is just rounded.
THat's what I am saying, it shouldn't be integer operation in the first place.

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.
__________________

Asus K8N (nForce3)
AMD64 3000+ (754)
768MB DDR 400MHz (PC 3200)
nVidia Geforce 6600GT 128MB (AGP)

Aquamark3 - 53,000

"Good friends mustn't always be together. It is the feeling of oneness when distant that proves a lasting friendship"

Syed Fawad is offline   Reply With Quote
Old April 15th, 2004   #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
Exophase is offline   Reply With Quote
Old April 16th, 2004   #11 (permalink)
-PM to advertise here-
 
Syed Fawad's Avatar
 
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)
__________________

Asus K8N (nForce3)
AMD64 3000+ (754)
768MB DDR 400MHz (PC 3200)
nVidia Geforce 6600GT 128MB (AGP)

Aquamark3 - 53,000

"Good friends mustn't always be together. It is the feeling of oneness when distant that proves a lasting friendship"

Syed Fawad is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:20.

© 2006 - 2008 Emu Forums | About Emu Forums | Legal | A member of the Crowdgather Forum Community


Powered by vBulletin® Version 3.7.0 Release Candidate 3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5