Really having trouble trying to draw a four leaf rose: This is the exercise:
Draw a picture of the “fourleaved rose” whose equation in polar coordinates is r =cos(2θ) . Let θ go from 0 to 2*pi in 100 steps. Each time, compute r and then compute the (x, y) coordinates from the polar coordinates by using the formula x = r ⋅ cos( θ) , y = r ⋅ sin(θ )
My Code:
public Rose(double awidth, double aheight)
{
width = awidth;
height = aheight;
theta = 0;
}
public void drawRose(Graphics2D g2)
{
Ellipse2D.Double test ;
double r = 0;
for(int i = 0; i <= 100; i++)
{
r = Math.cos(Math.toRadians(2*theta) );
x = r *( Math.cos( Math.toRadians(theta) ) * width ) + 300;
y = r * ( Math.sin( Math.toRadians(theta) ) * height ) + 300 ;
test = new Ellipse2D.Double(x, y, width, height);
theta += 3.6;
g2.draw(test);
}
}
}
Any help will be greatly appreciately.
Your biggest mistake is here:
You're creating 100 Ellipses with the points that are on the rose, but that with heights and widths of the desired rose. You really don't want 100 ellipses, but rather you want to connect lines between the x and y points you've created, that is connect the current x, y with the previous ones created (as long as there is a previous x and y).
One way is via these suggestions, but there are other ways to do this:
r
variable in the loopmoveTo(...)
method to add a starting pointlineTo(...)
method. This will draw a line between neighboring points.closePath()
on it.draw(path)
with the Graphics2D object, passing in your Path2D object.For example, this is created:
with this code:
Note that the key difference between my code and yours, other than the translations and the scaling is that I'm connecting the line between points created.