Jump to content

I have the task to write a c++ classes and the following questions have arised...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
tetris

tetris

    Newbie

  • Members
  • Pip
  • 1 posts
I have class Point, which two doubles, class Line, which contains two Points and class Triangle, which contains three Points. I have the following questions:
1) How to make construkor for the Line, which gets another Line, which is parallel to our line and Point which lies on our line?
2) How to make method of the class Line, which gets another Line and returns Point, which is the intersection of the two lines?
3) How to make method of the class Triangle,which gets another line and returns array of Points(if they exist), which are the intersection of the line and the triangle.If the are countless then returns only two points?

Edited by tetris, 20 March 2010 - 03:04 AM.


#2
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
1) find the slope of the line you get as parameter and use this value and the point you get in order to find another point on the line you're building. Two lines bering parallel means the have the same slope value.
2)A line can be viewed as an equation of the next form: y=mx+n. So for each line you will have to calculate the m (slope) and the n (value of y when x=0).
Now lets say you have l1: y=m1x+n1 and l2: y=m2x+n2. You want the point where m1x+n1=m2x+n2.
From here you can calculate x: x=(n2-n1)/(m1-m2). Once you have the x value, the y value is easy: y= m1(x value)+n1 or m2(x value)+n2. Doesnt matter which one you choose since at the intersection point both have the same value.
Notice though that you should check earlier that m1 is not equal to m2 - lines aren't parallel.
3)you can look at triangle as something that represents 3 lines. So you can apply step 2 to each line.