java - how do I calculate the points in a 5-point polygon -


I have a demo with a button and when the user clicks on a button called polygon, the polygon starts with the point Hit, this code works fine in drawing but unfortunately it pulls the polygon in the wrong place.

polygonal square

  square polygon shape {int x, y; Private polygon P; Public PolygonShape (int x, int y) {// X, Y sent to this constructor // The coordinates of the point where the user clicked this. X = x; this. Y = y; } Public Zero Draw (Graphics G) {p = New Polygon (); For (int i = 0; i & lt; 5; i ++) p.addPoint (int) (x + y * Math.cos (i * 2 * Math.PI / 5)), (int) ( X + y * Math.sin (i * 2 * monastery. Pi / 5))); G.drawpolygon (p);  x  and  y   

are the center of polygon, you are using them wrong (you want to add x in coordinate and y y in coordinate) and Instead of multiplying one more important variable: r for radius, instead of multiplying by y , you should type in the r Should multiply by.

In other words:

  square polygon shape {int x, y, r; Private polygon P; Public PolygonShape (int x, int y, int r) {this.x = x; this. Y = y; This.r = R; } // Provide the default radius of 100 pixels if no radius is given. Public PolygonShape (int x, int y) {this (x, y, 100); } Public Zero Draw (Graphics G) {p = New Polygon (); For (Int i = 0; I <5; i ++) {Double angle = I * 2 * Math PI / 5; P.addPoint ((int) (x + r * Math.cos (angle)), (int) (y + r * Math.sin (angle))); } G.drawPolygon (p); }}  

Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -