|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionDuring the development of a videogame skeleton, one of my biggest concerns was the drawing of a large interstellar map. Since it was just a skeleton, implementing 3D graphics with OpenGL or Direct3D was not an option. I soon discovered that GDI+ could very well fit to my needs. Next I had to face mainly two issues:
The included code shows how I solved those problems. It could be helpful to show how easy is to add graphics and shapes using GDI+ and .NET. Conversion between pixel geometry and logical-units geometryWhen we want to display something using GDI+, we must reason in terms of pixels. The If I must draw a line, say, from the point (10,10) to the point (30,30) I must call: //
// The main Graphics object
//
Graphics panelGraphics = this.panel.CreateGraphics();
panelGraphics.DrawLine(new Pen(Color.Black,0.2F),10,10,30,30);
Since every method and property of GDI+ is based on pixels, drawing items in terms of some other units of measure implies a conversion. Also, when we use "real world" formulae, they must be adapted to the "pixel" logic. Instead of spreading the conversions throughout the code, a class is what is needed. We could use the .NET library class
The
In the case above the //
// GridView initialized for a 400x500 pixel panel of 10.0 cm wide
//
GridView gv = new GridView(10.0,400,500);
Now, if you want to know how many pixels in this panel, a rectangle of 4.5 cm wide is, or if you want to know where the point (1.2cm,2.4cm) is, you may use: //
// Conversions
//
int pixelWidth = gv.getPhysicalWidth(4.5);
int pX = gv.getPhysicalX(1.2);
int pY = gv.getPhysicalY(2.4);
Drawing spheresWhen the An object of the class //
// Initializing Sphere object
//
Sphere s1 = new Sphere(gv,r,this.centerX,this.centerY);
s1.SphereColor=this.txtColor.BackColor;
where
The algorithm for drawing the sphere is very simple, and it is based on drawing arcs in a rectangle whose dimensions shrink. The algorithm draws the arcs first from right to left, then from top to bottom. private void drawArcs(Graphics g, Pen color, Rectangle r)
{
int x1=r.Left+r.Width/2;
int y1=r.Top;
int x2=x1;
int y2=r.Top+r.Height;
int x3=r.Left;
int y3=r.Top+r.Height/2;
int x4=r.Left+r.Width;
int y4=y3;
// draw axis
g.DrawLine(color,x1,y1,x2,y2);
g.DrawLine(color,x3,y3,x4,y4);
// right-left arcs
for (int j=r.Width; j>0; j-=10)
{
int left = r.Left+(r.Width-j)/2;
Rectangle rc = new Rectangle(left,r.Top,j,r.Height);
g.DrawArc(color,rc,0.0F,180.0F); // 0-180 degrees
g.DrawArc(color,rc,180.0F,360.0F); // 180-360 degrees
}
// top-bottom arcs
for (int j=r.Height; j>0; j-=10)
{
int top = r.Top+(r.Height-j)/2;
Rectangle rc = new Rectangle(r.Left,top,r.Width,j);
g.DrawArc(color,rc,270.0F,450.0F); // 270-90 degrees
g.DrawArc(color,rc,90.0F,270.0F); // 90-270 degrees
}
}
Modifying the number of loops (j-=10) you can obtain finer wires. You can also modify the width of the Point of InterestWhen you draw something on a
|
|||||||||||||||||||||||||||||||||||||||||||||||||||