Path: chuka.playstation.co.uk!news From: Toby Sargeant Newsgroups: scee.yaroze.programming.3d_graphics Subject: Re: Line normal Date: Fri, 18 Feb 2000 23:58:54 +1100 Organization: PlayStation Net Yaroze (SCEE) Lines: 118 Message-ID: <38AD420E.BF9ED486@fulcrum.com.au> References: <38ac28a2.9170426@news.playstation.co.uk> <38ae2f75.183732833@news.playstation.co.uk> <38ad3569.3035594@news.playstation.co.uk> NNTP-Posting-Host: modem20.csse.monash.edu.au Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.14 i686) X-Accept-Language: en Rad wrote: > > Hi Steven (or anyone else who can help!), > > Yeah, I'm refering to a 2D line. > > Say I have line A which is 64 units long and I want to create another > line (B) which is perpendicular (intersecting at 90 degrees) to line A > and is 256 units long 128 units on either side of line A (make any > sense? :-/) > > I'll try and draw a diagram: > > * > * > line B **************** > * > * > line A Firstly, it's almost always best to think of geometric primitives in parametric terms. This essentially means that the primitive is described in terms of one or more other variables which vary over a range. To represent a line parametrically, you need a point and a unit vector. The line is then described by the following teo equations: x(t)=v_x.t + p_x y(t)=v_y.t + y_y where (p_x,p_y) is a point that the line passes through (v_x,v_y) is the unit vector that describes the slope of the line t is the variable that the line is parameterised with respect to In this format, you can easily find a perpendicular vector by rotating the vector by 90 degrees. The two dimensional rotation matrix is: [+cos(r) -sin(r)] [+sin(r) +cos(r)] if r=pi/2 radians (for 90 degree rotation), this becomes: [+0 -1] [+1 +0] Multiplying this by the line vector, we get: [+0 -1][v_x] = [-v_y] [+1 +0][v_y] [+v_x] Therefore, the parametric equation perpendicular to the line we started with is: x'(t)=-v_y.t + p_x y'(t)= v_x.t + p_y now, what does this mean in the specific case you mentioned? firstly, how do you get a unit vector from a pair of points? if you have 2 points, p1 and p2, then a non unit vector parallel to the line is v = (p1_x-p2_x,p1_y-p2y) the length of that vector is (note that |v| is the magnitude (or length) of v): |v| = sqrt ((p1_x-p2_x)^2 + (p1_y-p2_y)^2) if we divide a vector by its scalar length, we get a unit vector: unit_vector(v) = (v_x/|v|,v_y/|v|) given a unit vector describing your line, then if t ranges over [0,1], then the line it describes is 1 unit long. Substituting t=-64 and t=+64 into the equations for the perpendicular line will therefore give you the two endpoints of the line that is perpendicular to the line you started with, is 128 units long, and has p as its midpoint. avoiding square roots and floating point calculations is a good idea. if you only want your output line to have a length that is a ratio of your input line, then you don't need to work with a unit vector. simply scale the vector by the appropriate amount, and and substiturt t=+1 and t=-1. in the case of a 64 unit line resulting in a 256 unit line, with 128 units on either side, you want to scale your input vector up by a factor of 2 (so that its length is 128), and then substitute. working through this: your input line is p1,p2 you want your perpendicular to pass through p1 (making a T) calculate the vector: v=(p1_x-p2_x,p1_y-p2_y) scale up by 2: v=(2.(p1_x-p2_x),2.(p1_y-p2_y)) the perpendicular line equations are: x(t)=-2.(p1_y-p2_y).t + p1_x y(t)= 2.(p1_x-p2_x).t + p1_y substitute t=-1,+1 for the line endpoints: p1'_x=2.(p1_y-p2_y)+p1_x p1'_y=-2.(p1_x-p2_x)+p1_y p2'_x=-2.(p1_y-p2_y)+p1_x p2'_y=2.(p1_x-p2_x)+p1_y > Why does a 3D line have an infinite number of lines normal to it? If you think about it, a plane in 3d space has a line normal. If you draw a line on that plane, then the line is perpendicular to the plane normal. You can draw infinitely many lines with different orientations on the plane, and all of them will be perpendicular to the plane normal. Toby.