Difference between revisions of "For Tutorial 5"

From Progzoo
Jump to: navigation, search
(Octagons)
(Less Regular Polygons)
Line 94: Line 94:
 
==Less Regular Polygons==
 
==Less Regular Polygons==
 
<question height="150" width="150" imgOut="flag.png" className="Raster" title="Less Regular">
 
<question height="150" width="150" imgOut="flag.png" className="Raster" title="Less Regular">
 +
You can make a different kind of polygon by changing the radius at each step...
 
<p class='imper'>Make the radius different for different values of i.</p>
 
<p class='imper'>Make the radius different for different values of i.</p>
[[Image:flaglessRegular.png]]<br/>
+
[[Image:flag8Points.png]]<br/>
 
<prog lang="java"><![CDATA[static void drawFlag(Graphics2D g)
 
<prog lang="java"><![CDATA[static void drawFlag(Graphics2D g)
 
{
 
{
 
   Polygon reg = new Polygon();
 
   Polygon reg = new Polygon();
   int r = 20;
+
   int r;
   int n = 8;
+
   int n = 6;
 
   for (int i=0;i<n;i++)
 
   for (int i=0;i<n;i++)
 
   {
 
   {
Line 119: Line 120:
 
{
 
{
 
   Polygon reg = new Polygon();
 
   Polygon reg = new Polygon();
   int r = 20;
+
   int r;
   int n = 8;
+
   int n = 16;
 
   for (int i=0;i<n;i++)
 
   for (int i=0;i<n;i++)
 
   {
 
   {
 
     double a = i*2*Math.PI/n;
 
     double a = i*2*Math.PI/n;
 +
    if (i %2 ==0)
 +
      r=40;
 +
    else
 +
      r=20;
 
     reg.addPoint((int)(r*Math.cos(a)),
 
     reg.addPoint((int)(r*Math.cos(a)),
 
                 (int)(r*Math.sin(a)));
 
                 (int)(r*Math.sin(a)));
Line 129: Line 134:
 
   g.setColor(Color.black);
 
   g.setColor(Color.black);
 
   g.translate(75,75);
 
   g.translate(75,75);
   g.drawPolygon(reg);
+
   g.fillPolygon(reg);
  g.translate(-20,-20);
+
  g.drawPolygon(reg);
+
  g.translate(0,40);
+
  g.drawPolygon(reg);
+
  g.translate(40,-40);
+
  g.drawPolygon(reg);
+
  g.translate(0,40);
+
  g.drawPolygon(reg);
+
 
}
 
}
 
]]>
 
]]>
 
</answer>
 
</answer>
 
</question>
 
</question>

Revision as of 13:52, 8 October 2012

Regular Polygon

You can find the coordinates of a regular polygon using sin and cosine.

Change n and r so that you get a hexagon that is twice the size.

Flaghexagon.png


[Font] [Default] [Show] [Resize] [History] [Profile]

Octagons

Draw the octogon 5 times to make the figure shown.

Flagoctagons.png


[Font] [Default] [Show] [Resize] [History] [Profile]


Less Regular Polygons

You can make a different kind of polygon by changing the radius at each step...

Make the radius different for different values of i.

Flag8Points.png


[Font] [Default] [Show] [Resize] [History] [Profile]