Archive
Categories
- Antivirus (4)
- Applications (5)
- Books and Articles (24)
- Computer Tutorials (10)
- Flash (3)
- Game (4)
- Graphic Design Tutorials (13)
- Internet Software (5)
- Internet Tutorials (12)
- Movie (8)
- Music (4)
- Novel Collections (5)
- Photoshop (10)
- Programming Tutorials (12)
- Template (3)
Tutorial : Drawing Advanced Shapes using C++
12:53 AM |
Diposkan oleh
Fiola |
Edit Post
For drawing a more advanced shape we need to introduce loops, this is a good link to understanding the fundamentals of loops. http://csharp.net-tutorials.com/basics/loops/. Right so the advanced shape we are going to produce is a star.
To draw a star we need a formula which will allow us to create the angle to make everything connect. The equation is:
- 4.0 * Math.PI / 5.0.
And also points to pivot off which are defined by this equation:
- x + starLength * Math.Cos(angle);
This is what the form should look like before we start coding.
It is just a simple button and a picturebox.
Starting off we are going to initialise 5 variables above the form1 function, these are:
Graphics paper;
int length = 20;
int x = 100;
int y = 100;
double angle;
The Graphics paper allows us to draw the shape, with x and y setting a place in the picturebox where we are going to start drawing the star.
Next in the form1() function add this below InitializeComponent():
paper = pictureBox1.CreateGraphics();
This sets the graphics to the picturebox.
Now it is time for the main components of this program. The for loop, this is where everything happens.
private void button1_Click(object sender, EventArgs e)
������� {
����������� for (int i = 0; i < 5; i++)
����������� {
��������������� double calculation = 4.0 * Math.PI / 5.0;
��������������� angle += calculation;
��������������� int x1 = Convert.ToInt32(x + length * Math.Cos(angle));
��������������� int y1 = Convert.ToInt32(y + length * Math.Sin(angle));
��������������� paper.DrawLine(Pens.Black, x, y, x1, y1);
��������������� x = x1;
��������������� y = y1;
����������� }
������� }
������� {
����������� for (int i = 0; i < 5; i++)
����������� {
��������������� double calculation = 4.0 * Math.PI / 5.0;
��������������� angle += calculation;
��������������� int x1 = Convert.ToInt32(x + length * Math.Cos(angle));
��������������� int y1 = Convert.ToInt32(y + length * Math.Sin(angle));
��������������� paper.DrawLine(Pens.Black, x, y, x1, y1);
��������������� x = x1;
��������������� y = y1;
����������� }
������� }
OK ill go through everything here, we set up a for loop which has a maximum loop count of 5. This means we go through the loop 5 times before it exits it. Then we do the angle calculation and add the calculation to the angle.
Just a quick note, that angle is a global variable which means that it holds its data for as long as the program is run. Whereas the calculation variable is local and loses its data every time the program loops.
Its time to set up a new location for the other end of the line. So we add the current position of x then add the length we want and the angle of the edge. Once we have that we draw the line and at the end we make the start point of the edge equal the end and go back to the top and start the loop again.
Once done and you press the draw button on your form it should look like this.
Label:
Programming Tutorials,
Tutorial
Subscribe to:
Post Comments (Atom)
0 komentar:
Post a Comment