Есть треугольник.
Нужно поворачивать его.
Задаю я его так.
3 точки.
Есть координаты curX и curY это координаты реальные на экране.
А x1,y1, x2,y2, x3,y3 - это координаты треугольника относительно точки (0,0)
которая при выводе треугольника равна curX, xurY
Как повернуть треугольнк скажем по часовой стрелки, например вокруг этой точки (0,0)
Вот у меня что...
package mainpack; import java.awt.Color; import java.awt.Graphics; import java.math.*; import java.util.Random; public class NewThread implements Runnable{ Thread t; Thread mainTh; // данные полигона boolean isLife = true; // цвет Color polColor = Color.black; public void setColor(Color x) { this.polColor = x; } public Color getColor() { return this.polColor; } // количество точек int PointCount=3; // координаты точек protected PointXY thePoint[] = new PointXY[PointCount]; // данные о движении int curX=0,curY=0; public int speedUp=0, speedLeft=0; public double xan=0.001; //нужно ли поворачивать boolean isRotate = false; // действия над объектом public void moveUpDown() { if (this.curY > 0) { this.curY = this.curY - speedUp; } } public void moveLeftRight() { if (this.curX >0) { this.curX = this.curX - speedLeft; } } public void draw(Graphics g) { g.drawLine(this.curX+thePoint[0].getX(),this.curY + thePoint[0].getY(), this.curX +thePoint[1].getX() , this.curY +thePoint[1].getY()); g.drawLine(this.curX+thePoint[1].getX(),this.curY + thePoint[1].getY(), this.curX +thePoint[2].getX() , this.curY +thePoint[2].getY()); g.drawLine(this.curX+thePoint[2].getX(),this.curY + thePoint[2].getY(), this.curX +thePoint[0].getX() , this.curY +thePoint[0].getY()); } NewThread(int x1,int y1,int x2,int y2,int x3,int y3) { // инициализация полигона thePoint[0] = new PointXY(x1,y1); thePoint[1] = new PointXY(x2,y2); thePoint[2] = new PointXY(x3,y3); Random rnd = new Random(); this.curX = rnd.nextInt(100); this.curY = rnd.nextInt(100); if (curX < 20){curX*=3;} if (curY < 20){curY*=3;} //создание потока t = new Thread(this,"Polygon"); //запуск потока t.start(); } public void calc() { for (int i=0; i<=2; i++) { double x = this.thePoint[i].getX(); double y = this.thePoint[i].getY(); double r = Math.sqrt( x*x + y*y); x = r*Math.cos(xan); y = r*Math.cos(xan); this.thePoint[i].setX((int)Math.round(x)); this.thePoint[i].setY((int)Math.round(y)); } xan+=0.001; } public void run() { try { // описание поведения объекта while (isLife) { moveUpDown(); moveLeftRight(); //if (this.isRotate) {calc(); } calc(); Thread.sleep(100); } } catch (InterruptedException e) { //System.out.println("Interrupt"); } //System.out.println("Halt"); } }
Метод calc должен вычислять поворот каждой точки, но почему то треугольник быстро уползает куда то а не поворачивается.