Visual Basic 6.0 Guide
Drawing Angles Project
This project is the basic code required to place 2 lines at a specific angle to each other. The main uses for this code would be when you want to animate an arc using timer controls or if you want to create a ticking clock (see clock project for a variation on this theme). I like this problem because it requires a creative and mathematical (albeit only basic trigonometry). I had to do a few sketches to work out how to do this because there are a fair few factors to take into consideration.
The image below is a screenshot from this basic program. You enter an angle and the red line is placed at that angle from the black line.
Lines in VB are drawn using co-ordinates on the containing object (in this case a picture box). Each line has 4 properties, X1, Y1, X2, Y2 which are used to make up the co-ordinates of the start and end of the line. The X1 and Y1 values of both lines are the same. The trick is to calculate the values for X2 and Y2 on the second, in this case, red line. To do this you need to imagine a triangle superimposed on the above screenshot. In the image below, the green lines are used to form our imaginary triangle. The red line is the hypotenuse of the imaginary triangle and is labelled as h. Our angle, labelled Θ is known because side a (adjacent to the angle) of our triangle is parallel to the original line. The letter o is used to represent the side opposite to the angle.
In the diagram above some of the variables are known. Side h is the same length as the length of the original line. The angle Θ is the angle that we wish to draw. Because side o is perpendicular to the original line, we know that this is a right-angled triangle. Any other information about the triangle can be calculated using some basic but forgettable trigonometry. Once we know the value for a, we can add this to the value for X1 to work out the new value for X2. A similar process is used to work out the value for Y2 from the value for o.
In a right-angled triangle,
In Vb, trigonometric functions are calculated in radians. You need to multiply an angle in degrees by pi / 180 to convert it into radians.
You would need to rethink this triangle if your original lines are not horizontal (see clock project). It is best to create a subroutine that will plot any angle sent to it and use a timer to call the subroutine with a different angle each time. Command buttons can be used to start and stop the timer as well as to reset the angle.
Download Drawing Angles Project - angles.zip Approx 2kb.