Thread: Triangle and Line Segment Intersection

  1. #31
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Since you're only creating one cube, we don't need [1]. We can do the following:
    Code:
    cube myCube;
    
    myCube.v0.x = 0;
    myCube.v7.y = 0;
    /* And so on. */
    Or...
    Code:
    scanf("%f", &myCube.v0.x);
    You've got it!
    Last edited by JDGATX; 04-08-2008 at 03:37 PM.

  2. #32
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    yes, thanks. your implementation of DoesIntersect is perfect. The only confusion was this:
    Before DoesIntersect is ever called in the main, a user inputs the vertices of a cube.
    These are operated on by a totally separate function to form output_vertices.

    then the output vertices are used to determine the triangles and linesegments representing the faces and edges of the output_cube.

    it is these triangles and linesegments (from output_cube) that are sent to the DoesIntersect function from the main.

    But, yeah, your implementation of DoesIntersect is perfect.

    Thanks

  3. #33
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Glad to help. Post here if you have any more trouble with this program. I think you have a good start, though!

  4. #34
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Yikes, sorry... one more question...

    earlier you used a loop to input (with k as loop variable):

    within this, I need something to the effect of "&myCube.v[k].x" rather than "&myCube.v0.x"

    It doesnt seem my current structure will allow this.

    What about this:

    Code:
    typedef struct
    {
    	double x;
    	double y;
    	double z;
    } point;
    
    typedef struct
    {
    	point p1;
    	point p2;
    	point p3;
    } triangle;
    
    typedef struct
    {
    	point p1;
    	point p2;
    } segment;
    
    typedef struct
    {
    	point v0;
    	point v1;
    	point v2;	
    	point v3;
    	point v4;
    	point v5;
    	point v6;
    	point v7;
    } cube;
    
    int DoesIntersect(triangle tri, segment seg);
    
    
    // main
    int main(void)
    {
    	triangle tri;
    	segment seg;
    	int result;
    	
    	int j;
    	char input_buffer[160];
    	int check_input;
    	double u;
    
    	cube input_cube; 
    
    	for ( j = 0; j < 8; j++)
    		{
    			printf("Input vertex coordinates x y z: ");
    			fflush(stdout);
    			fgets(input_buffer, sizeof input_buffer, stdin);
    			check_input = sscanf(input_buffer, "&#37;f %f %f", &input_cube.[j].x, &input_cube.[j].y, &input_cube.[j].z);
    
    			if (check_input !=3)
    			{
    				printf("Improper format.");
    				return 0;
    			}
    		}
    Last edited by hebali; 04-08-2008 at 03:54 PM.

  5. #35
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    I understand the dilemma completely.

    Unfortunately, we can't have "variable" variable names. v0 must always be v0, v1 must always be v1. So in this sense, if we keep the current structures and the current implementation of our input calls, we need to add a line for each vertex v0, v1, ..., v8.

    So you would have one sscanf() for each vertex v0, v1, ..., v8.

    We could venture into the land of pointers and attempt to do it that way (by having a pointer to our cube structure and then using our loop variable to somehow increment our pointer through the structure and its sub-structures, or "points"), but we'd quickly be approaching the limits of my capability. I'm sure we could stumble through it (I have a decent idea on how we would do it), but it might be a bit too complex for our purposes.

    Back when I said we needed a loop, I was under the impression you were asking the user to input EIGHT triangles.

    Now, we're just inputting ONE cube. So we can ditch the loop and simply have 8 sscanf() statements and their accompanying prompts and buffers (similar to what I did for prompting the user for each point in the triangle, except now you're asking the user for each point of the cube). And if you're concerned about keeping your main() relatively clean, once you have the functionality installed, you can go back and modularize further by creating a "UserPrompt" or "UserInput" function.

    *** BIG EDIT ***

    Oh! Idea!

    You could create an array of vertices in your cube data structure like this:
    Code:
    typedef struct
    {
         point vertices[8];
    } cube;
    Then, you could loop around as before:
    Code:
    for (j = 0; j < 8; j++)
    {
         scanf("&#37;f", &myCube.vertices[j].x, &myCube.vertices[j].y, &myCube.vertices[j].z);
    }
    I'm feeling pretty dumb for not catching myself on that earlier. Try that!
    Last edited by JDGATX; 04-08-2008 at 04:18 PM.

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, cube can be defined as:
    Code:
    typedef struct
    {
    	point v[8];
    } cube;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #37
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by matsp View Post
    Of course, cube can be defined as:
    Code:
    typedef struct
    {
    	point v[8];
    } cube;
    --
    Mats
    Yeah. As I said, felt pretty dumb about missing that.

  8. #38
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    you both rule

  9. #39
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Question:

    Let's say I've got the structs in place as seen above. And then I implement a triangle (which contains 3 points: x, y, z)

    If I perform operations with this, can I work with all three points simultaneously?

    So for instance,

    Lets say I want to subtract the value of each coordinate of Triangle B from the corresponding coordinates of Triangle A.

    Could I do something like tri[1] - tri[0] ?

    Or does it have to be
    tri[1].x - tri[0].x
    etc

    thanks

  10. #40
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Your best bet would probably be to implement a class in C++ and then define the operators in such a manner that allows you to do normal subtraction and addition and so on.

  11. #41
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by hebali View Post
    Question:

    Let's say I've got the structs in place as seen above. And then I implement a triangle (which contains 3 points: x, y, z)

    If I perform operations with this, can I work with all three points simultaneously?

    So for instance,

    Lets say I want to subtract the value of each coordinate of Triangle B from the corresponding coordinates of Triangle A.

    Could I do something like tri[1] - tri[0] ?

    Or does it have to be
    tri[1].x - tri[0].x
    etc

    thanks

    As IceDane says, the only way to do this would be to migrate to C++ and utilize its "overloading" functionality. This would cause you to re-write the '-' operator as a new function, a new function which would take in two triangles, use the dot notation to manually subtract all of the elements individually as you are currently doing, and return a new triangle object.

    Then, thanks to C++, your statement of tri[1] - tri[0] would work.

    My knowledge of C++ is limited, but a Google should help you find out how to overload the '-'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Draw a triangle
    By Hikaru in forum C++ Programming
    Replies: 8
    Last Post: 08-13-2006, 02:26 PM
  3. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  4. normals for triangle strips (oGL)
    By Perspective in forum Game Programming
    Replies: 8
    Last Post: 04-01-2003, 06:59 PM
  5. Replies: 1
    Last Post: 01-30-2002, 01:04 PM