Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perhaps a trigonometry question

Status
Not open for further replies.

l310564

Programmer
Jun 18, 2006
50
GB
I'm having a little problem with triangles. I have two points on a form with a line drawn between them. What I want to do is place a triangle in the middle pointing to point A from point B, where ever that maybe on the form.

This is more of a maths question than a programing one but how do you workout the three coordinates of the triangle?

I hope someone can help.

Cheers,

Hugh

If knowlege can create problems it is not through ignorance that we will solve them.

Isaac Asimov
 
l310564,

By saying "What I want to do is place a triangle in the middle pointing to point A from point B" do you mean you want to draw the triangle in the middle of line or something else?

How much length of each side of triangle do you want to draw?

Where do you want to draw triangle?

I'm sorry that I could not understand you. If you can explain it with some diagram or rephrase your problem statement to explain where do you want to draw triangle, may be I can help you out.

 
Thank you for your reply computerjin. I have got a little further with the code since yesterday but I'm still having problems working out the correct angle.

The Problem:
I have a line on a form that i want to place a triangle in the middle of so i can show the direction of it. This line can start and end anywhere on the form. The way i have been trying to achieve this is to draw a triangle and then rotate it to the correct angle. Unfortunately i can't work out the correct angle to rotate it to.

I hope this code will explain better what I'm trying to ac hive, if not please ask me because this is driving me insane.


cheers,

Hugh

Code:
 Public Sub drawline(ByVal startlineX As Integer, ByVal startlineY As Integer, ByVal endlineX As Integer, ByVal endlineY As Integer)
        'trangle points
        Dim points(2) As Drawing.PointF
        points(0).X = (startlineX + endlineX) / 2
        points(0).Y = (startlineY + endlineY) / 2
        points(1).X = ((startlineX + endlineX) / 2) + 20
        points(1).Y = ((startlineY + endlineY) / 2) + 20
        points(2).X = ((startlineX + endlineX) / 2) + 20
        points(2).Y = ((startlineY + endlineY) / 2) - 20


        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red, 4)
        Dim formGraphics As System.Drawing.Graphics
        formGraphics = Me.CreateGraphics()

        'create line on which to place trangle
        formGraphics.DrawLine(myPen, startlineX, startlineY, endlineX, endlineY)

     
       

    
        'try to work out the rotation
        Dim _rotationAngle As Double

        Dim opositelength As Double
        Dim ajacentlength As Double

        If startlineY = endlineY Then
            If startlineX > endlineX Then
                _rotationAngle = 180
            Else
                _rotationAngle = 0
            End If
        ElseIf startlineX = endlineX Then
            If startlineY > endlineY Then
                _rotationAngle = 270
            Else
                _rotationAngle = 90
            End If
        ElseIf startlineY > endlineY Then

            If startlineX > endlineX Then
                opositelength = startlineY - endlineY
                ajacentlength = startlineX - endlineX
                _rotationAngle = 180 + (1 / Math.Tan(opositelength / ajacentlength))
            Else
                opositelength = startlineY - endlineY
                ajacentlength = endlineX - startlineX
                _rotationAngle = 90 + (1 / Math.Tan(opositelength / ajacentlength))
            End If
        ElseIf startlineY < endlineY Then
            If startlineX > endlineX Then
                opositelength = endlineY - startlineY
                ajacentlength = startlineX - endlineX
                _rotationAngle = 180 + (1 / Math.Tan(opositelength / ajacentlength))
            Else
                opositelength = endlineY - startlineY
                ajacentlength = endlineX - startlineX
                _rotationAngle = 180 - (180 - (1 / Math.Tan(opositelength / ajacentlength)))
            End If
        End If

        'draw trangle and rotate to relevant position

        Dim max As System.Drawing.Drawing2D.Matrix
        max = formGraphics.Transform
        max.RotateAt(_rotationAngle, points(0))
        formGraphics.Transform = max
        formGraphics.FillPolygon(Brushes.Red, points)
        formGraphics.ResetTransform()
    End Sub

If knowlege can create problems it is not through ignorance that we will solve them.

Isaac Asimov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top