Project line from movable box at angle

evocube

Member
Joined
Apr 26, 2009
Messages
9
Programming Experience
1-3
I am trying to project a line from the center of a movable box(picturebox) at a specific angle . I get a line projection from the box at a radian angle but cannot get it to project from the center of the box and i need the angle based off the top of a second picture box. And would love a tutorial on how to contain the line in the picture box and when it comes to the border of the picture box reflect at the same angle as the original.
Here is my code that projects a line on the form not within the picture box.
VB.NET:
 Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        ' Draw line in picture box at radian angle

        Dim myPen As New System.Drawing.Pen(Color.Black)
        Dim formGraphics As System.Drawing.Graphics
        Dim cx1 As New Integer
        Dim cy1 As New Integer
        Dim p2x As New Integer
        Dim p2y As New Integer
        p2x = PictureBox2.Location.X
        p2y = PictureBox2.Location.Y

        cy1 = Convert.ToInt32(Math.Cos(p2x) * 50)

        cx1 = Convert.ToInt32(Math.Sin(p2y) * 50)
        formGraphics = Me.CreateGraphics()
        formGraphics.DrawLine(myPen, p2x, p2y, cx1, cy1)
        myPen.Dispose()
        formGraphics.Dispose()




    End Sub
Picture box 2 is teh one the line is projected from.

Thanks.
 
First up, NEVER draw on any control except in that control's OnPaint method or a handler for its Paint event. The reason is that, every time a Paint event is raised, all previous drawing is erased. You need to draw on the Paint event so that your drawing is restored each time the control is repainted. Any time you need to force a repaint to refresh the drawing, you call Refresh or Invalidate and Update on the appropriate control. Below are a couple of links that demonstrate how to draw on the Paint event and also how to extend a drawing across multiple controls.

Very Simple Drawing Program

Draw Common "Picture" on All Controls
 
Well i see the error on that side but am still lost on how to draw a line at an angle from a reference line (edge of picture box). I will start re writing my program and see where I get with it based off the links. I actually found a few really good gdi+ tutorials i just have yet to find one that incorperates angles.

Thanks again I will rewrite today and post the project.
 
It's simple trigonometry. If you have two points then the have the ends of the hypotenuse of a right-angled triangle. You can easily calculate everything else about that triangle using trig, which is implemented via the Math class. You should start by drawing a picture, i.e. with a pen and paper. Draw your two points and then the rest of the triangle and you should be able to see the basic maths involved.
 
Back
Top