Tuesday, August 4, 2009

Mapouka Reference Watch Online

Draw lines to form a circle with

We have seen the basic pricipios draw lines and we have drawn a quadrilateral . We will use these same principles to draw a circle using the same code by simply changing the method display () .

circle drawn with JOGL.



What we will do first is to draw a hexagon. To achieve our mission, we draw 6 vertices as follows:
  1. The first corner as we draw to the coordinate (4, 0).
  2. We rotate the apex 60 º (360 º / 6) and we redraw in its new coordinates.
  3. repeat the previous step 5 times to draw a total of 6 vertices that form our hexagon.
Recall the formula for rotating a point angle , the new point be defined by:




We help an array of float size [6] [2], which represent our 6 coordinates, the array will initialize in the constructor.

Within the methods glBegin () and glEnd () declare our vertices:

gl.glBegin (GL.GL_LINE_LOOP)
/ / Initialize the first vertex
/ / x
point [0] [0] = 4.0F;

/ / coordinate and
point [0] [1] = 0.0f;

/ / Draw the first vertex
gl.glVertex2d (point [0] [0], item [0] [1]);

/ * Draw the 5 remaining vertices by rotating
* above 60 º , use the equivalent in radians *
as the functions sin and cos of
* Java Math package works with the latter * /

for (int i = 1; i \u0026lt;6; i + +)
{
/ / x '= x * cos (60 °) - y * sin (60 º)
point [i] [0] = point [i-1] [0] * Math.cos (Math.toRadians (60)) -
point [i-1] [1] * Math.sin (Math.toRadians (60));


/ / y '= x * sin (60 º) + y * cos (60 º)
point [i] [1] = point [i-1] [0] * Math.sin (Math.toRadians (60)) +
point [i-1] [1] * Math.cos (Math.toRadians (60));


/ / Draw the new vertex obtained
gl.glVertex2d (point [i] [0], item [i] [1]);}

gl.glEnd ();

When you compile and run the modified code shows a result as follows:

Hexagon drawn with JOGL.

now return to modify the code, but now, to draw an octagon. Use the same method used above, only this time instead of rotating 60 degrees (360 º / 6) we will rotate the top 45 º (360 º / 8) and the vertex we draw 8 times.
We
help of a float array of dimensions [8] [2], which represent our 8 coordinates, the array will initialize in the constructor.

Within the methods glBegin () and glEnd () declare our vertices:

gl.glBegin (GL.GL_LINE_LOOP)
/ / Initialize the first vertex
/ / x
point [0] [0] = 4.0F;

/ / coordinate and
point [0] [1] = 0.0f;

/ / Draw the first vertex
gl.glVertex2d (point [0] [0], item [0] [1]);

/ * We draw the 8 remaining vertices by rotating
* above 45 degrees, use the equivalent
* radians as the functions sin and cos of
* package Math Java works with recent * /

for (int i = 1; i \u0026lt;8; i + +)
{
/ / x '= x * cos (45 degrees) - and * sin (45 º)
point [i] [0] = point [i-1] [0] * Math.cos (Math.toRadians (45) ) -
point [i-1] [1] * Math.sin (Math.toRadians (45));


/ / y '= x * sin (45 º) + y * cos (45 º)
point [i] [1] = point [i-1] [0] * Math.sin (Math.E toRadians (45)) +
point [i-1] [1] * Math.cos (Math.toRadians (45));


/ / Draw the new vertex obtained
gl.glVertex2d (point [i ] [0], item [i] [1]);}

gl.glEnd ();

When you compile and run the modified code shows a result like this

drawn with JOGL Octagon.

As can be seen in the examples above, if we continue to expand the sides of the figures, these are becoming more the shape of a circle. Modify the code again but this time to draw a circular polygon 360. Use the same method used above, only this time instead of rotating 45 degrees (360 º / 8) vertex we will rotate only 1 º (360 º / 360) and the vertex we draw 360 times.

We help of a float array of dimensions [360] [2], which represent our 360 coordinates, it will initialize array within the constructor.

Within the methods glBegin () and glEnd () declare our vertices:

gl.glBegin (GL.GL_LINE_LOOP)
/ / Initialize the first vertex
/ / x
point [0] [0] = 4.0F;

/ / coordinate and
point [0] [1] = 0.0f;

/ / Draw the first vertex
gl . glVertex2d (point [0] [0], item [0] [1]);

/ * Draw the 359 remaining vertices by rotating
* above 1, we use the equivalent
* radians as the functions sin and cos of
* package Math Java works with recent * /

for (int i = 1, i \u0026lt; , 360; i + +)
{
/ / x '= x * cos (1 °) - and * sin (1 °)
point [i] [0] = point [i-1] [0 ] * Math.cos (Math.toRadians (1)) -
point [i-1] [1] * Math.sin (Math.toRadians (1));


/ / y '= x * sin (1 °) + y * cos (1 °)
point [i] [1] = point [i-1] [0] * Math.sin (Math.toRadians (1))
+ point [i-1] [1] * Math.cos (Math.toRadians (1));


/ / Draw the new vertex obtained
gl.glVertex2d (point [i] [0] point [i] [1]);}

gl.glEnd ();

When you compile and run the modified code shows a result as follows:

Circumference drawn with JOGL.

And here we have our circle made with JOGL, as the sides are so close and so small they are almost imperceptible giving the appearance of a circle. This JOGL because we did not offer specific functions to draw circles.

If you wish to download the source code of the figures shown in the examples given just click on any of the following links:

Download dibujarHexagono.java source
dibujarOctagono Download source code . java
Download dibujaCircunferencia.java source

FINAL NOTE
The examples use a two-dimensional array to draw the vertices this was just to make it understandable. Just as a recommendation, to save memory in the system, do not use an array because the larger it will occupy more memory. Instead of using an array bidemensional, simply use two variables called, say, coordenadaX and sobreescribánlas coordenadaY and for each cycle using two auxiliary variables. Download the following source code to see the right thing to do:

dibujaCircunferencia.java Download source code without using two-dimensional array.

0 comments:

Post a Comment