Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

math experts?

Status
Not open for further replies.

keizersoz

Programmer
Apr 22, 2004
67
0
0
BE
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);
}
}
 
Well, this has little or nothing to do with Java, but anyway, that's a simple trigonometric rule.

You can take a look at for example.

You want to draw a line of length r with an angle, and you want to find out in which point the line ends (x,y) to pass it to the drawLine method.

Then
x=r·cos(angle)
y=r·sin(angle)

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top