001/*
002
003*/
004
005package renderer.models;
006import  renderer.scene.*;
007
008/**
009   Create a wireframe model of a circle in the xy-plane centered at the origin.
010*/
011public class Circle extends Model
012{
013   /**
014      Create a circle in the xy-plane with radius 1 and
015      with 12 line segments around the circumference.
016   */
017   public Circle( )
018   {
019      this(1, 12);
020   }
021
022
023   /**
024      Create a circle in the xy-plane with radius {@code r}
025      and with 12 line segments around the circumference.
026
027      @param r  radius of the circle
028   */
029   public Circle(double r)
030   {
031      this(r, 12);
032   }
033
034
035   /**
036      Create a circle in the xy-plane with radius {@code r}
037      and with {@code n} line segments around the circumference.
038
039      @param r  radius of the circle
040      @param n  number of line segments in the circle's circumference
041   */
042   public Circle(double r, int n)
043   {
044      super();
045
046      if (n < 3) n = 3;
047
048      // Create the circle's geometry.
049
050      // An array of vertices to be used to create the line segments.
051      Vertex[] v = new Vertex[n];
052
053      // Create all the vertices.
054      for (int i = 0; i < n; i++)
055      {
056         double c = Math.cos(i*(2.0*Math.PI)/n);
057         double s = Math.sin(i*(2.0*Math.PI)/n);
058         v[i] = new Vertex(r * c, r * s, 0);
059      }
060
061      // Create the line segments around the circle.
062      for (int i = 0; i < n - 1; i++)
063      {
064         addLineSegment(v[i], v[i+1]);
065      }
066      addLineSegment(v[n-1], v[0]);
067/*
068      // Or, we could build the circle the
069      // following, more efficient, way.
070      addVertex(v);
071      for (int i = 0; i < n - 1; i++)
072      {
073         addLineSegment(i, i+1);
074      }
075      addLineSegment(n-1, 0);
076*/
077   }
078}//Circle