I need help to draw a Polygon

user3503112

I am trying to draw a certain type of polygon but i can't manage to get it to work. Are my formula wrong ? And if so, can you help me?

I want to draw a polygon using Java.awt.polygon


TMP = new Polygon();

// this.getX() and this.getY() return an int
// consider getX() and getY() as x0 and y0

int x1,x2,x3,x4;
int y1,y2,y3,y4;

x1 = (int)  Math.round(this.getX() - this.getLength() * Math.cos(this.getAngle()));
y1 = (int)  Math.round(this.getY() - this.getLength() * Math.sin(this.getAngle()));

x3 = (int)  Math.round(x1 - this.getWidth() * Math.cos((Math.PI / 2) - this.getAngle()));
y3 = (int)  Math.round(y1 + this.getWidth() * Math.sin((Math.PI/2) - this.getAngle()));

x4 = (int)  Math.round(this.getX() - this.getWidth() * Math.cos((Math.PI/2) - this.getAngle()));
y4 = (int)  Math.round(this.getX() + this.getWidth() * Math.sin((Math.PI/2) - this.getAngle()));

x2 = (int)  Math.round((x1 + x3) / 2 + Math.cos(this.getAngle()) * this.getLength() / 2);
y2 = (int)  Math.round((y1 + y3) / 2 + Math.sin(this.getAngle()) * this.getLength() / 2);

TMP.addPoint(this.getX(), this.getY());
TMP.addPoint(x1, y1);
TMP.addPoint(x2, y2);
TMP.addPoint(x3, y3);
TMP.addPoint(x4, y4);
this.setPolygon(TMP);

This is what i expect https://i.gyazo.com/bdd8510d0128bab6347bb6c110f03c92.png

And i'm getting this https://i.gyazo.com/55a00ae07d4f31732db12805fa592aec.png

user3503112

My formulas were right. My values for length and widths were incorrect. My width was my length and mylength was my width. Sorry!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related