1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5
10 public class Circle extends BasicShape {
11
12 private final RPoint center;
13 private double r;
14 private int resolution;
15 private int[] sXPoints;
16 private int[] sYPoints;
17
18
24 public Circle(double x, double y, double r) {
25 super();
26 this.center = new RPoint(x, y);
27 this.r = r;
28 initialize();
29 }
30
31
38 public Circle(double x, double y, double z, double r) {
39 super();
40 this.center = new RPoint(x, y, z);
41 this.r = r;
42 initialize();
43 }
44
45
48 private void initialize() {
49 this.resolution = 360;
50 this.sXPoints = new int[resolution];
51 this.sYPoints = new int[resolution];
52 }
53
54
59 public Circle setR(double r) {
60 this.r = r;
61 redraw();
62 return this;
63 }
64
65
69 @Override
70 protected void drawShape(Graphics g, Matrix a) {
71 Vector rp = new Vector(); SPoint sp = new SPoint(0,0);
73 for (int i = 0; i < resolution; i++) {
74 a.transform(this.r*Math.cos((2*Math.PI/resolution)*i)+this.center.getX(),
75 this.r*Math.sin((2*Math.PI/resolution)*i)+this.center.getY(),
76 this.center.getZ(),rp,sp);
77 sXPoints[i] = sp.getX();
78 sYPoints[i] = sp.getY();
79 }
80 g.drawPolygon(sXPoints, sYPoints, resolution);
81 }
82 }
83