PDA

View Full Version : Sleeper: The two lines problem



gplawton
03-08-2005, 01:55 PM
Option Explicit

Dim l1p1x As Single, l1p1y As Single
Dim l1p2x As Single, l1p2y As Single
Dim l2p1x As Single, l2p1y As Single
Dim l2p2x As Single, l2p2y As Single

Sub twolines()
'get the coordinates (abscissa and the ordinate)
getthedata
'for each pair of points, determine whether they actually defined
Dim line1isdefined As Boolean
line1 isdefined = "isline Defined(1)"
If line1defined Then
MsgBox ("line 1 is defined")
Else
MsgBox (" line 1 is not defined")
End If
End Sub

Sub getthedata()
l1p1x = Range("line1 point1 xvalue")
l1p1y = Range("line1 point1 yvalue")
l2p1x = Range("line2 point1 xvalue")
l2p1y = Range("line2 point1 yvalue")
l1p2x = Range("line1 point2 xvalue")
l1p2y = Range("line1 point2 yvalue")
l2p2x = Range("line2 point2 xvalue")
l2p2y = Range("line2 point2 yvalue")
End Sub

mdmackillop
03-08-2005, 02:22 PM
Hi, Welcome to VBAX.
Not sure what your problem is but...


Dim line1isdefined As Boolean 'This is True/False
line1 isdefined = "isline Defined(1)

Is the space intentional? You are setting your Boolean to a String type

If line1defined Then 'Is this a typo for line1isdefined?

gplawton
03-08-2005, 02:34 PM
This is the what I was given to work with for the two lines problem and i cannot figure it out

The Two Lines Problem

1. Get the coordinates (abscissa and ordinate) of four points

2. Validate that each abscissa and ordinate is numeric


For each pair of points, determine whether they actually define a line. If a pair of coordinates is the same point, then you do not have a line. Depending on the input, the user might define zero, one, or two lines.


4. For each defined line
a. Calculate its slope. The slope of a vertical line is infinity.
b. Calculate its y-intercept. Vertical lines do not have a Y-Intercept.
c. Display the equation of the line. The equation of a vertical line is ?X = c? where ?c? is a constant (the abcissa from a point on the line). The equation of a horizontal line is ?Y = c? where ?c? is a constant (the ordinate from a point on the line).

5. If the user defined two lines, determine and display whether lines are intersecting, parallel, or collinear. If the lines intersect, then calculate and display the following:
a. The coordinates of the point of intersection
b. The angle between the lines
c. Determine and display whether or not the line segments intersect.

Background, Definitions, Formulas


Cartesian Plane: a two-dimensional plane having all points defined by Cartesian coordinates
Coordinates: The pair of values known together as the coordinates of a point are known separately as the abscissa and the ordinate of that point.

? Abscissa is the horizontal or x-coordinate of a point. The distance from the vertical axis or y-axis measured along a line parallel to the horizontal axis or x-axis.

? Ordinate is the vertical or y-coordinate of a point. The distance from the x-axis measured along a line parallel to the y-axis.

Point S
X-axis
Point R
Point T
Point Q
Y-axis
Line L1
Line L2

This diagram illustrates the scenario. Four points Q, R, S, and T are positioned on the Cartesian coordinate plane identified by the X and Y axes.

Let the coordinates of point Q be (Xq, Yq) and let the other three points be defined similarly. Furthermore, let the points Q and R define line L1 and points S and T define line L2.

Equations of the Lines, Calculating the Slope and Y-intercept

The general equation of a line on the Cartesian plane: Y = mX + b

The equation of line 1 is

The slope of a line is the ratio of the difference in the Y values for two points on the line divided by the difference in the X values for the same two points (?the rise over the run?), unless the line is vertical in which case the slope of the line is infinity. In this case, a denominator of zero means the line is vertical and has infinite slope. If you are modeling this in VBA or in Excel, you must provide logic to prevent division by zero.

The slope of line 1

The Y-intercept of a line is the point where the line intersects the vertical, or Y-axis. If the line is vertical, then the Y-intercept is undefined.

The Y-intercept of line 1

Build the equations for line L2 in a similar fashion.

Determining the Relationship Between the Lines (only if two lines are defined)

? If the slopes are equal and the Y-intercepts are also equal, then the lines are collinear

? If the slopes are equal but the Y-intercepts are not, then the lines are parallel

? If the slopes of two lines are not equal, then the two lines intersect

Calculating the Coordinates of the Intersection Point (only if the lines intersect)

If the lines intersect, then there is one point where the equations of the two lines are equal. To find that intersection point and the angle between the lines, begin by setting the two equations equal to each other.

This leads to

That will give the abscissa or X-coordinate of the intersection point. Substitute that value of X in the standard equation of either line to find Y.

Calculating the Intersection Angle (only if the lines intersect)

The tangent of an angle is the ratio of the sine and cosine of that angle. The slope of a line relative to the positive horizontal axis is the same as the tangent of the angle made by that line and the horizontal axis. From trigonometry, we know that the arctangent of the tangent of an angle is the angle. If M1 and M2 are the two slope values, then define a and b to be the angles of the respective lines relative to the horizontal axis. Calculate aand bas follows:

a = arctangent (M1) and b = arctangent (M2)

The angle between the two lines is the absolute value of the difference, a - b:

angleOfIntersection = Abs (a - b)

Solving for Line Segment Intersection using Parametric Equations

The parametric equations of a line defined by points p and q are:

x = px + t (qx - px) and y = py + t (qy - py)

? If t is between zero and one (inclusive) 0 <= t <= 1
then the point (x, y) lies on the line segment pq

? If t > 1, the point (x, y) lies beyond the point q

? If t < 0, the point (x, y) lies beyond the point p

Two line segments pq and rs intersect if and only if the point of intersection is between or on one of the end points of both line segments. Using t1 and t2 as parameters for pq and rs respectively and setting the associated equation pairs equal to each other:

two equations in x: px + t1 (qx - px) = rx + t2 (sx - rx)

two equations in y: py + t1 (qy - py) = ry + t2 (sy - ry)

Solve for the two unknowns t1 and t2. The two line segments intersect if the solution satisfies the relations

0 <= t1 <= 1 and 0 <= t2 <= 1

One technique to solve for the intersection of two lines is to rearrange the above equations into matrix format and use Cramer?s rule to solve for t1 and t2. This looks like the following:

t1 (qx - px) - t2 (sx - rx) = rx - px

t1 (qy - py) - t2 (sy - ry) = ry - py

can be written as


Using Cramer?s rule: (note: detRHS means the determinant of the Right Hand Side)

If detRHS is zero, the lines are parallel and there is no solution. If the lines are collinear, there are an infinite number of solutions. If detRHS is not equal to zero and both t1 and t2.are between zero and one, inclusive, then the line segments intersect.[1] (http://www.vbaexpress.com/forum/showthread.php?t=2223#_ftn1)

Some Useful Trigonometry Relations

tan(x) = sin(x)/cos(x).
arctangent (tangent(a)) = a
If x = Arctan(y) then y = tan(x), -π/2 < x < π /2,
If y = tan(x) then x = Arctan(y) + π n, where n is an arbitrary integer.

Conversion from radians to degrees and degrees to radians:


2p radians = 360 degrees. To convert degrees to radians, multiply degrees by pi/180 or use the RADIANS function. To convert radians to degrees, multiply radians by 180/pi or use the DEGREES function
if a is an angle in radians then or is the same angle in degrees


Excel / VBA Trig Functions (excerpted from the Excel and VBA help files)

Excel has two built in arctangent functions:


ATAN(number)

Returns the arctangent, or inverse tangent, of a number. Number is the tangent of the angle you want. The arctangent is the angle whose tangent is number. The returned angle is given in radians in the range -pi/2 to pi/2.


ATAN2(x_num,y_num)

Returns the arctangent, or inverse tangent, of the specified x- and y-coordinates. The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a point with coordinates (x_num, y_num). The angle is given in radians between -pi and pi, excluding -pi.

A positive result represents a counterclockwise angle from the x-axis; a negative result represents a clockwise angle.

ATAN2(a,b) equals ATAN(b/a), except that a can equal zero in ATAN2.

If both x_num and y_num are zero, ATAN2 returns the #DIV/0! error value.

VBA also has a built-in arctangent function:

Atn(number)

Returns a Double specifying the arctangent of a number. The required number argument (http://javascript<strong></strong>:hhobj_4.Click()) is a Double (http://javascript<strong></strong>:hhobj_5.Click()) or any valid numeric expression (http://javascript<strong></strong>:hhobj_6.Click()). The Atn function takes the ratio of two sides of a right triangle (number) and returns the corresponding angle in radians. The ratio is the length of the side opposite the angle divided by the length of the side adjacent to the angle. The range of the result is -pi (http://javascript<strong></strong>:hhobj_7.Click())/2 to pi/2 radians.

Point-Line Relationships

The two-point form equation of a line passing through points p and q is:

? A point r lies on the line pq if fpq(r) = 0. (pq is a vector)

? A point r lies on the on the left side of line pq if fpq(r) > 0.
Therefore, r is on the left side of pq if it forms a positive angle from the vector pq.

? A point r lies on the on the right side of line if fpq(r) < 0.

fpq(r) ? (qx-px) (ry-py) - (qy-py) (rx-px) = (for 2D vectors)

To avoid division by zero for vertical or horizontal lines, rewrite the equation as

fpq(x, y) ?(qx- px)(y- py) - (qy- py)(x- px) = 0

The following statements are equivalent:

Ÿ The point r is on the left of the directed line .

Ÿ Points p, q, and r are oriented in an anti-clockwise sense.

Ÿ The triangle pqr is positively oriented. fpq(r) > 0

? The area of the triangle pqr is .

? The line equation can be written in determinant form as

fpq(r) = = (qxry - qyrx) + (pyrx - pxry) + (pxqy - pyqx)

= qx ry - pyqx - pxry - qyrx +pxqy + pyrx

= (qx ry - pyqx - pxry + pxpy) - (qyrx-pxqy - pyrx + pxpy)

= (qx-px) (ry-py) - (qy-py) (rx-px)

The orientation of the triplet pqr is:

? clockwise if fpq(r) < 0

? collinear if fpq(r) = 0

? anti-clockwise if fpq(r) > 0

The line pq divides the entire plane into two half planes. Two points, r and s, are on the same side of a line , if they belong to the same half-plane.

? if fpq(r) fpq(s) > 0, r and s lie on the same side of a line

? if fpq(r) fpq(s) < 0, r and s lie on opposite sides of a line

Given that two lines intersect, use parametric (or vector) equations of lines to determine if the line segments intersect.

The intersection (http://mathworld.wolfram.com/Intersection.html) of two lines (http://mathworld.wolfram.com/Line.html) L1 and L2 in two dimensions with, L1 containing the points (x1, y1) and (x2, y2), and L2 containing the points (x3, y3) and (x4, Y4), is given by

X =


Where denotes a determinant (http://mathworld.wolfram.com/Determinant.html)

Y =


This corresponds to simultaneously solving


= 0
and
= 0


for x and y.



An example: Given the following system of simultaneous equations:

x + 2y = 2 and 3x + 4y = 8

Write this pair of equations in matrix notation as:

Let A = and B =

1. Find the determinant of the matrix of coefficients : Det (A) = (1 x 4) - (2 x 3) = 4 - 6 = -2

2. Solve for x. Replace the elements in the first column of Matrix A with the elements in matrix B (since the elements in the first column of Matrix A are the coefficients of x) to obtain the new matrix. Call this new matrix Ax.

Ax = and the determinant of Ax = Det (Ax) = (2 x 4) - (2 x 8) = -8

3. Solve for y. Replace the elements in the second column of Matrix A with the elements in Matrix B (since the elements in the second column of Matrix A are the coefficients of y) to obtain another matrix. Call this Matrix Ay.

Ay = and the determinant of Ay =Det (Ay) = (1 x 8) - (2 x 3) = 8 - 6 = 2

Therefore, the two lines intersect at the point (4, -1)

Proof: Substitute the values of x = 4 and y = -1 into the original equations:

Eq. 1.

x + 2y = 2

4 + 2(-1) = 2

4 - 2 = 2

2 = 2 check

Eq. 2.

3x + 4y = 8

3(4) + 4(-1) = 8

12 - 4 = 8

8 = 8 check

Another example: Solve the following pair of equations using the Cramer's Rule:

7x + 8y = 120 and 6x + 9y = 98

Rewrite the pair of equations in matrix form:

i) Build matrices A, B, Ax and Ay.

ii) Det(A) = (7 x 9) - (8 x 6) = 63 - 48 = 15

iii) Det (Ax) = (120 x 9) - (8 x 98) = 1080 - 784 = 296.

iv) Det(Ay ) = (7 x 98) - (120 x 6) = 686 - 720 = -34

v) X = Det(Ax) / Det(A) = 296 / 15 and Y = Det(Ay) / Det(A) = -34 / 15

Proof: Substitute x = 296 / 15 and y = -34 / 15 into the original equations:

Eq. 1. 7x + 8y = 120 7(296 / 15) + 8(-34 / 15) = (2072 ? 272) / 15 = 120

Eq. 2. 6x + 9y = 98 6(296 / 15) + 9(-34 / 15) = (1176 ? 306) / 15 = 98

[1] (http://www.vbaexpress.com/forum/showthread.php?t=2223#_ftnref1)[1]detRHS can also be calculated as the cross product of and :

The determinant of the matrix will be zero if the angle between the lines is zero. This means that the lines are parallel and there is no solution or that the lines are collinear and there are an infinite number of solutions (no unique solution). If the magnitude (length) of either line segment is zero, then the determinant is zero. This happens if the coordinates of the two points for a line are identical (i.e. a line was not defined).

Most of what things you replied about are typos. I have a hard enough time w/ spell check. Being a "newbie" (I laughed pretty hard when I read that. Humor is essential) and trying to figure this out on my own is extremely difficult.

mdmackillop
03-08-2005, 02:36 PM
Hi
This appears to be a "homework" problem which under our tems and conditions we cannot help you with. We might be able to help with the coding aspects, such as the points I made previously, but not with the logic of your code.
MD

gplawton
03-08-2005, 02:54 PM
I appreciate your help. I am a little ignorant about the terms and conditions. If I may I would like to call on you tomorrow because it is homework and it's due today. If I can't get it to run will you help?

mdmackillop
03-08-2005, 03:11 PM
Here is the site policy http://www.vbaexpress.com/forum/faq.php?faq=psting_faq_item#faq_hom_faq_item

johnske
03-08-2005, 03:19 PM
Hi
This appears to be a "homework" problem which under our tems and conditions we cannot help you with. We might be able to help with the coding aspects, such as the points I made previously, but not with the logic of your code.
MD


Hi gplawton,

While I can sympathise with your predicament, I'll expand a little on what MD said... if we do homework or assignments for others this is a form of "cheating" that we will not knowingly partake in. I can only advise you to see your tutor/lecturer/teacher for whatever help they are willing to give, or ask for an extension...

Kind regards,
John

Anne Troy
03-08-2005, 03:36 PM
glawton: It's tough to say it nicely and not have people try to stretch it, but the bottom line at 90% of the forums I frequent, I will GIVE GUIDANCE AND DIRECTION if I see someone making similar mistakes, or if they ask about a specific problem that is often a problem for anyone, then I may point toward learning about the solution--perhaps a keyword that you may need. I think most of us feel this way. So, don't go away 'cause it's homework, but try very hard to work it out yourself, and let us know when you hit a wall.

johnske
03-08-2005, 06:57 PM
Hi gplawton,

I had a much closer look at what you want and it appears that it's a mathematical problem that you're maybe trying to do plotted examples for using VBA.

Following on in the vein of MD and Dreamboat said, without going into the maths side of it I can point out several problems with your actual code[vba]


Sub twolines()
'get the coordinates (abscissa and the ordinate)
getthedata
'for each pair of points, determine whether they actually defined
Dim line1isdefined As Boolean '< can only be true or false (MD)
line1isdefined = "isline Defined(1)" '< RHS is neither true nor false (MD)
If line1 isdefined Then '< you have a space here, line1 is not declared (MD)
MsgBox ("line 1 is defined")
Else
MsgBox (" line 1 is not defined")
End If
End Sub

Sub getthedata()
'you need to declare the variables in the sub unless you
'mean them to be public variables. In which case you
'would put them up top (as you had them), but you then
'need to use Public instead of Dim...
Dim l1p1x As Single, l1p1y As Single
Dim l1p2x As Single, l1p2y As Single
Dim l2p1x As Single, l2p1y As Single
Dim l2p2x As Single, l2p2y As Single
l1p1x = Range("line1 point1 xvalue") '< I assume these are named ranges
l1p1y = Range("line1 point1 yvalue")
l2p1x = Range("line2 point1 xvalue")
l2p1y = Range("line2 point1 yvalue")
l1p2x = Range("line1 point2 xvalue")
l1p2y = Range("line1 point2 yvalue")
l2p2x = Range("line2 point2 xvalue")
l2p2y = Range("line2 point2 yvalue")
End Sub

HTH,
John