I copied the paint method from a book. In the function drawline they used following calculations:
int dx = (int)(r * Math.cos(angle * Math.PI / 180));
int dy = (int)(r * Math.sin(angle * Math.PI / 180));
Does someone has an idea what is exactly the meaning of these calculations?
FULL METHOD:
public class DrawPoly extends Frame
{
public void paint(Graphics g)
{ int r = 45; // radiuns of circle bounding PacMan(R)
int cx = 50; // center of that circle
int cy = 100;
int angle = 30; // opening angle of mouth
int dx = (int)(r * Math.cos(angle * Math.PI / 180));
int dy = (int)(r * Math.sin(angle * Math.PI / 180));
g.drawLine(cx, cy, cx + dx, cy + dy); // lower jaw
g.drawLine(cx, cy, cx + dx, cy - dy); // upper jaw
g.drawArc(cx - r, cy - r, 2 * r, 2 * r, angle,
360 - 2 * angle);
Polygon p = new Polygon();
cx = 150;
int i;
for (i = 0; i < 5; i++)
p.addPoint(
(int)(cx + r * Math.cos(i * 2 * Math.PI / 5)),
(int)(cy + r * Math.sin(i * 2 * Math.PI / 5)));
g.drawPolygon(p);
Polygon s = new Polygon();
cx = 250;
for (i = 0; i < 360; i++)
{ double t = i / 360.0;
s.addPoint(
(int)(cx + r * t * Math.cos(8 * t * Math.PI)),
(int)(cy + r * t * Math.sin(8 * t * Math.PI)));
}
g.drawPolygon(s);
}
}
int dx = (int)(r * Math.cos(angle * Math.PI / 180));
int dy = (int)(r * Math.sin(angle * Math.PI / 180));
Does someone has an idea what is exactly the meaning of these calculations?
FULL METHOD:
public class DrawPoly extends Frame
{
public void paint(Graphics g)
{ int r = 45; // radiuns of circle bounding PacMan(R)
int cx = 50; // center of that circle
int cy = 100;
int angle = 30; // opening angle of mouth
int dx = (int)(r * Math.cos(angle * Math.PI / 180));
int dy = (int)(r * Math.sin(angle * Math.PI / 180));
g.drawLine(cx, cy, cx + dx, cy + dy); // lower jaw
g.drawLine(cx, cy, cx + dx, cy - dy); // upper jaw
g.drawArc(cx - r, cy - r, 2 * r, 2 * r, angle,
360 - 2 * angle);
Polygon p = new Polygon();
cx = 150;
int i;
for (i = 0; i < 5; i++)
p.addPoint(
(int)(cx + r * Math.cos(i * 2 * Math.PI / 5)),
(int)(cy + r * Math.sin(i * 2 * Math.PI / 5)));
g.drawPolygon(p);
Polygon s = new Polygon();
cx = 250;
for (i = 0; i < 360; i++)
{ double t = i / 360.0;
s.addPoint(
(int)(cx + r * t * Math.cos(8 * t * Math.PI)),
(int)(cy + r * t * Math.sin(8 * t * Math.PI)));
}
g.drawPolygon(s);
}
}