Page 1 of 1
Java assignment help
Posted: Mon Feb 05, 2007 8:35 pm
by Kaos4
Hi,
I got this Java problem as a school assignment and I have been working on it for hours, but I have gotten practically nowhere. Any help would be appreciated. Here is the problem:
Given and int number of sides and a double radius draw a filled regular polygon that is centered.
I know the formulas to find area, interior angles, and such for regular polygons, but I am not sure how to implement it. My ideas are to draw a triangle and rotate it by 360/n degrees n times. I know I need to find a pattern between the width coordinates and height coordinates, but I am having trouble doing this.
Like I said before, any help would be appreciated.
Thanks
Posted: Tue Feb 06, 2007 3:34 am
by Maveritchell
Forgive me; it's been a while since I've done some Java programming (that, and I hated it), so most of the notation will probably be wrong, and I can't remember the specific Java operations for the trig functions (sin, cos, tan, etc.) Let me know if you need clarification. And when you say radius, I assume you mean from the center of the regular polygon to the vertex of one of its "triangular" sides, and not from the center to the midpoint of a side.
This part should just be used to find the dimensions of the triangle that you need to rotate (and you will need to rotate it "sides" times).
((sides-2) * 180) / sides == int insangle
2 * radius * sin(0.5*insangle) == int tribaselength
radius * cos(0.5*insangle) == int triheight
//Draw that triangle, and then make another copy, rotated "insangle" degrees,
//and in total, create "sides" number of triangles.
---
That should work, but I really only gave you basic geometry. If you're looking for something more, please be more specific.
Posted: Tue Feb 06, 2007 8:58 am
by Adreniline
do you just mean one set of " = " as the assignment operator? the " == " is the test of equivalence operator
Posted: Tue Feb 06, 2007 11:43 am
by Maveritchell
Yeah, that's what I meant. Whoops. Never did like Java.
Posted: Tue Feb 06, 2007 3:10 pm
by ShadowHawk
Moved to Technical Advice
Posted: Tue Feb 06, 2007 6:02 pm
by Kaos4
I created a triangle that changes with amount of sides and radius using this:
int n = 10; //number of sides
double r = w/6; //radius
int rint = (int)r;
int angle = ((n-2)*180)/n ; //interior angles
int apothem = (int)(r * Math.cos(Math.PI/n)); //apothem length
double s = 2*r*Math.sin(Math.PI/n); //side length
int side = (int)s;
int a = 360 / n; //central angles
int insangle = (int)(((s-2) * 180) / s);
int xCoords[] = {w/2, w/2 - side/2,w/2 + side/2} ;
int yCoords[] = {h/2, h/2 - apothem,h/2 - apothem}; //creates the triangle
g.setColor(Color.red);
g.fillPolygon(xCoords, yCoords, 3);
The triangle is an upside down triangle, and i want to rotate it at the vertex. So I should rotate it by a, or the central angle. I created a loop to do this, but I am not sure how to get the points on my triangle to rotate properly. I found some formula on the internet but I seem to be using it wrong. This is what I have so far:
for (int i = 1; i <= n; i++)
{
xCoords[1] = (int)((w/2 - side/2)*Math.cos(a)-(h/2 - apothem)*Math.sin(a));
xCoords[2] = (int)((w/2 + side/2)*Math.cos(a)-(h/2 - apothem)*Math.sin(a));
yCoords[1] = (int)((w/2 - side/2)*Math.sin(a)+(h/2 - apothem)*Math.cos(a));
yCoords[2] = (int)((w/2 + side/2)*Math.sin(a)+(h/2 - apothem)*Math.cos(a));
g.fillPolygon(xCoords, yCoords, 3);
a = a + a;
}
thanks again for your help.
Posted: Tue Feb 06, 2007 9:20 pm
by Kaos4
I've also been trying something like this:
xTemp[0] = xCoords[2];
xCoords[1] = xTemp[0] ;
xCoords[2] = ;
yTemp[0] = yCoords[2];
yCoords[1] = yTemp[0];
yCoords[2] = ;
This would work to switch the 2nd x and y coordinates with the first, but I am having trouble coming up with the new second x and y coordinates.
Posted: Wed Feb 07, 2007 1:38 am
by t551
OR, you could use Java's handy dandy polygon class.
I have some source code that was made to draw a flag for a lab, on which I had to draw stars. Stars are a bit more comlex, obviously, but the principle is the same. Also, the code is optimized to make a five-point star, you'll have to figure a way to change that.
The values of 54 and 18 were values away from 90/270 and 0/180 degrees, for each point. You'll need to come up with a way to generate those values for each situation.
Code: Select all
public void drawStar(Graphics graph, int posX, int posY, int outerR, int innerR, int r, int g, int b)
{
int [] x = new int [10];
int [] y = new int [10];
x[0] = (int) posX;
x[1] = (int) Math.round(posX+Math.cos(54 * (Math.PI/180))*(innerR));
x[2] = (int) Math.round(posX+Math.cos(18 * (Math.PI/180))*(outerR));
x[3] = (int) Math.round(posX+Math.cos(18 * (Math.PI/180))*(innerR));
x[4] = (int) Math.round(posX+Math.cos(54 * (Math.PI/180))*(outerR));
x[5] = (int) posX;
x[6] = (int) Math.round(posX-Math.cos(54 * (Math.PI/180))*(outerR));
x[7] = (int) Math.round(posX-Math.cos(18 * (Math.PI/180))*(innerR));
x[8] = (int) Math.round(posX-Math.cos(18 * (Math.PI/180))*(outerR));
x[9] = (int) Math.round(posX-Math.cos(54 * (Math.PI/180))*(innerR));
y[0] = (int) posY-outerR;
y[1] = (int) Math.round(posY-Math.sin(54 * (Math.PI/180))*(innerR));
y[2] = (int) Math.round(posY-Math.sin(18 * (Math.PI/180))*(outerR));
y[3] = (int) Math.round(posY+Math.sin(18 * (Math.PI/180))*(innerR));
y[4] = (int) Math.round(posY+Math.sin(54 * (Math.PI/180))*(outerR));
y[5] = (int) posY+innerR;
y[6] = (int) Math.round(posY+Math.sin(54 * (Math.PI/180))*(outerR));
y[7] = (int) Math.round(posY+Math.sin(18 * (Math.PI/180))*(innerR));
y[8] = (int) Math.round(posY-Math.sin(18 * (Math.PI/180))*(outerR));
y[9] = (int) Math.round(posY-Math.sin(54 * (Math.PI/180))*(innerR));
Color myColor = new Color(r,g,b);
graph.setColor(myColor);
Polygon star = new Polygon();
for (int count=0; count<10; count++)
{
star.addPoint(x[count], y[count]);
}
graph.fillPolygon(star);
/*graph.drawLine(posX, posY, posX, posY);
graph.drawLine(x[0], y[0], x[0], y[0]);
graph.drawLine(x[2], y[2], x[2], y[2]);
graph.drawLine(x[4], y[4], x[4], y[4]);
graph.drawLine(x[6], y[6], x[6], y[6]);
graph.drawLine(x[8], y[8], x[8], y[8]);
graph.drawLine(x[1], y[1]);
graph.drawLine(x[3], y[3]);
graph.drawline(x[5], y[5]);
graph.drawLine(x[7], y[7]);
graph.drawLine(x[9], y[9]);*/
}
Posted: Fri Feb 09, 2007 12:12 am
by [RDH]Zerted
This is programming, there are many ways to do this. Instead of generating and rotating all those triangles, you can create your shape by connecting points.
You know the amount of sides required, so you can figure out the angle each line has off the line next to it (lines which come from the center point 0,0 to the intersection of two sides). The length of each line is already known, because I'm assuming that is what you mean by radius.
Create a Polygon object (its a Java class).
In a loop, fakingly create these lines. You will need some trig to get each endpoint. Take the line's endpoint (where two sides meet) and add it as a point to your polygon object. When the loop ends, you should have your completed polygon. The polygon will be centered at 0,0 which is the top left pixel. Just translate (a function) it to where ever you want the center to be. There are many ways to fill this Shape in.