Difference between revisions of "For Tutorial 5"

From Progzoo
Jump to: navigation, search
(Octagons)
(Octagons)
Line 61: Line 61:
 
   g.fillPolygon(reg);
 
   g.fillPolygon(reg);
 
   g.translate(2*r,2*r);
 
   g.translate(2*r,2*r);
 +
  g.fillPolygon(reg);
 +
}
 +
]]></prog>
 +
<answer lang="java"><![CDATA[static void drawFlag(Graphics2D g)
 +
{
 +
  Polygon reg = new Polygon();
 +
  int r = 20;
 +
  int n = 8;
 +
  for (int i=0;i<n;i++)
 +
  {
 +
    double a = i*2*Math.PI/n;
 +
    reg.addPoint((int)(r*Math.cos(a)),
 +
                (int)(r*Math.sin(a)));
 +
  }
 +
  g.setColor(Color.black);
 +
  g.translate(75,75);
 +
  g.drawPolygon(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>
 +
</question>
 +
 +
 +
==Less Regular Polygons==
 +
<question height="150" width="150" imgOut="flag.png" className="Raster" title="Less Regular">
 +
<p class='imper'>Make the radius different for different values of i.</p>
 +
[[Image:flaglessRegular.png]]<br/>
 +
<prog lang="java"><![CDATA[static void drawFlag(Graphics2D g)
 +
{
 +
  Polygon reg = new Polygon();
 +
  int r = 20;
 +
  int n = 8;
 +
  for (int i=0;i<n;i++)
 +
  {
 +
    double a = i*2*Math.PI/n;
 +
    if (i==0 || i=2 || i==4)
 +
      r=40;
 +
    else
 +
      r=20;
 +
    reg.addPoint((int)(r*Math.cos(a)),
 +
                (int)(r*Math.sin(a)));
 +
  }
 +
  g.setColor(Color.black);
 +
  g.translate(75,75);
 
   g.fillPolygon(reg);
 
   g.fillPolygon(reg);
 
}
 
}

Revision as of 13:49, 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

Make the radius different for different values of i.

File:FlaglessRegular.png


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