Advertisement

line representation in c++

Started by December 30, 2002 08:00 PM
7 comments, last by Seriema 22 years, 1 month ago
Hi there! I''m making a mathlib in C++ for me to use in games :D Right now I''m making a c2DLine, and going to do a c3DLine. But I''m having one tough decisionanxeity, representation! This is what I''ve wrote down, but I can''t decide... * Basic: x = a or y = a * Slope-Intercept: y = mx + b (where m is the slope, m = (y1-y0)/(x1-x0) = tan(a). a = arctan( (y1-y0) / (x1-x0) ) = arctan(m) in the intervall -PI/2 < a < PI/2) * Point-Slope: y-y0 = m(x-x0) * General: ax + by + c = 0 * Parametric: x = x0 + (x1-x0)*t and y = y0 + (y1-y0)*t any tips? If you didn''t understand I''ll gladly babble some more "No lies of sugar can sweeten the sournes of reality" }+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
Well, I don''t know about DX, but if you''re speaking of drawing a line, just use glBegin(GL_LINES). If you''re using it for like collision detection, the point-slope form works well (y = mx + b). For instance, if you want to find out if two lines intersect, and where you can set them equal to each other.
Advertisement
since this is a mathlib it doesn''t matter if you''re using OGL, DX, SDL, GDI, or whatever. This is just the math part

How should I ask the user to insert the data? ask for m and b ?

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
For 2D lines I would probably go for a normal/distance structure (analogous to planes in 3D). For a 3D line, either two points or point/direction parametric versions will be more useful.
Often there''s a difference between a line and a line segment. For a line, I normally use a point on the line and a direction vector. For a line segment, I normally use 2 points - the start and end of the segment.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
so... that means I should have a c2DLine and a c2DLineSegment ?

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
Advertisement
Definately use the vector format for a line:

Lx,y,z) = (x1,y1,z1) + t(dx,dy,dz)

Where (x1,y1,z1) is a point, t is the independent variable, and (dx,dy,dz) is the direction vector for the line.

This format is very useful, especially when it comes to detecting the intersection of a line with planes, spheres, etc.
If you are working with lines and planes, another alternative would be to store the normal and the distance. The distance represents how far the line/plane is from the origin along the normal. It''s a lot better for doing intersection checks because a lot of the information you need to do that stuff, such as the normal and the distance, are right there. Then again, it depends on how you are going to be doing your intersection checks. I use a vector-based method, so those two pieces of information are crucial.
maybe should''ve said it, but... I''m making Point, Line, Vector, Plane, Matrix, and more classes

This was the "line" class

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]

This topic is closed to new replies.

Advertisement