Android Studio How to draw a hexagon

Viewed 91

I was trying to draw a hexagon on a canvas. But I have not found something that could help me. I wasn't able to do it on my one. My problem is that the hexagon line points aren't drawing. The green dots are the circles I want to draw and the black lines should be the hexagon when I draw it with lines How I want it to look like (I'm sorry for my bad drawing skills)

My Hexagon class

public class Hexagon extends Rotateables {

    public float radius;

    public Hexagon(Game game, Vector2 position, float radius) {
        super(game, position.x, position.y, 0, 0, 0);
        this.radius = radius;
    }

    public void drawHexagon(Canvas canvas) {
        for (int angle = 0; angle < 360;angle += 45) {
            setAngle(angle);
            Vector2 drawPos = getMovement();

            Paint paint;
            paint = new Paint();
            paint.setColor(Color.GREEN); 
            
            canvas.drawCircle(drawPos.getX(), drawPos.getY(), radius, paint);
        }
    }


}

My Rotateables class

public abstract class Rotateables {

    protected double x ,y , angle; // angle
    protected int w, h; // width and height

    // constructor
    public Rotateables(double x, double y, double angle, int w, int h) {

        this.x = x;
        this.y = y;
        this.angle = angle;
        this.w = w;
        this.h = h;

    }
    // returning all the necessary value of this class

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public double getAngle() {
        return angle;
    }

    public int getW() {
        return w;
    }

    public int getH() {
        return h;
    }

    // setting the angle
    public void setAngle(int aa) {

        angle = Math.toRadians(aa);
    }

    // move toward the angle
    // //forward
    public void moveForward() {
        x += Math.cos(angle);
        y += Math.sin(angle);
    }

    public Vector2 getMovement() {
        double x = Math.cos(angle) + this.x;
        double y = Math.sin(angle) + this.y;
        return new Vector2(x,y);
    }

    public void moveForward(float speed, boolean drawVertex) {
        x += Math.cos(angle)*speed;
        y += Math.sin(angle)*speed;
    }
    //backward
    public void moveBackward() {
        x -= Math.cos(angle);
        y -= Math.sin(angle);
    }

    public void setAngle(Point p1)
    {
        double xDiff = x - p1.x;
        double yDiff = y - p1.y;
        double _angle = Math.toDegrees(Math.atan2(yDiff, xDiff));
        
        setAngle((int)_angle);
    }

    public static double getAngle(Point p1, Point p2)
    {
        double xDiff = p2.x - p1.x;
        double yDiff = p2.y - p1.y;
        return Math.toDegrees(Math.atan2(yDiff, xDiff));
    }
}
0 Answers
Related