Search code examples
javaswingtrigonometryjava-2djapplet

trying to make a star using sine and cosine


so I'm trying to make a star for a program where I am going to draw a flag. I'm pretty much limited to the Java Applet class. since the process should be the same for every star with a different center, I figured I would pass the center to a method with X and Y values, and then draw it based off the angle from the center using Polygon. The problem is the shapes that are coming out are not in any way stars.

public void paint( Graphics g)
{
    super.paint( g );
    Polygon star= new Polygon();
    int radius=20;
    int roundX=(int)Math.round(radius*Math.cos(Math.toRadians(54)));
    int roundY=(int)Math.round(radius*Math.sin(Math.toRadians(54)));
    int originX=100,originY=100;
    //int radius=20;
    int topY=originY+radius;
    int bottomLeftX=originX+roundX;
    int bottomY=originY-roundY;
    int middleY=originY+(int)Math.round(radius*Math.sin(Math.toRadians(18)));;
    int midRightX=originX+(int)Math.round(radius*Math.cos(Math.toRadians(18)));;
    int midLeftX=originX-(int)Math.round(radius*Math.cos(Math.toRadians(18)));
    int bottomRightX=originX+roundX;
    int bottomRightY=middleY-roundY;
    star.addPoint(originX, originY);
    star.addPoint(originX, topY);
    star.addPoint(bottomLeftX, bottomY);
    star.addPoint(midRightX, middleY);
    //star.addPoint(midLeftX, middleY);
    //star.addPoint(bottomRightX, bottomY);
    //star.addPoint(originX, topY);
    g.drawPolygon(star);
}

so the bottom left point should be 234 degrees from the center or 54 degrees from the x axis. when I code this either of these in and change the operation(+ or minus) it seems to shoot in the opposite direction than what I intended. The middle points should be 18 degrees above the center, though from the way it looks they look almost in line with the x axis. I've been disabling the points one by one to see if I can tweak it to get closer to a star, but at this I've realized that's the equivalent of sculpting while blindfolded and drunk.

if you could help me figure out what's wrong, I would greatly appreciated it.


Solution

  • Maybe you want a five-pointed star.

    private double sin(int degree) {
        return Math.sin(Math.toRadians(degree));
    }
    
    private double cos(int degree) {
        return Math.cos(Math.toRadians(degree));
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Polygon star = new Polygon();
    
        int radius = 20;
        int originX = 100;
        int originY = 100;
        int innerRadius = (int) Math.round(radius * sin(18) / sin(126));
    
        Point[] points = new Point[11];
        // top center 
        points[0] = new Point();
        points[0].x = 0;
        points[0].y = -radius;
        // inner top left
        points[1] = new Point();
        points[1].x = -(int) Math.round(innerRadius * cos(54));
        points[1].y = -(int) Math.round(innerRadius * sin(54));
        // top left 
        points[2] = new Point();
        points[2].x = -(int) Math.round(radius * cos(18));
        points[2].y = -(int) Math.round(radius * sin(18));
        // inner bottom left
        points[3] = new Point();
        points[3].x = -(int) Math.round(innerRadius * cos(18));
        points[3].y = (int) Math.round(innerRadius * sin(18));
        // bottom left
        points[4] = new Point();
        points[4].x = -(int) Math.round(radius * cos(54));
        points[4].y = (int) Math.round(radius * sin(54));
        // inner bottom center
        points[5] = new Point();
        points[5].x = 0;
        points[5].y = innerRadius;
        // bottom right
        points[6] = new Point();
        points[6].x = (int) Math.round(radius * cos(54));
        points[6].y = (int) Math.round(radius * sin(54));
        // inner bottom right
        points[7] = new Point();
        points[7].x = (int) Math.round(innerRadius * cos(18));
        points[7].y = (int) Math.round(innerRadius * sin(18));
        // top right
        points[8] = new Point();
        points[8].x = (int) Math.round(radius * cos(18));
        points[8].y = -(int) Math.round(radius * sin(18));
        // inner top right
        points[9] = new Point();
        points[9].x = (int) Math.round(innerRadius * cos(54));
        points[9].y = -(int) Math.round(innerRadius * sin(54));
        // top center
        points[10] = new Point();
        points[10].x = 0;
        points[10].y = -radius;
    
        for (int i = 0; i < points.length; i++) {
            star.addPoint(originX + points[i].x, originY + points[i].y);
        }
        g.drawPolygon(star);
    
    }