我想根据角度从段创建一个矩形。例如,在图片上:
C = xA + rectangleWidth
我需要找到角度CAD来旋转C并制作一个矩形
此代码可以使用70%的时间,但有些奇怪的事情发生了我不理解
case polyRectangle:
int rectangleWidth=25;
double vectorAngle, basicRotationAngle, rotationAngle;
int re, im, x1rot, y1rot, x2rot, y2rot;
double PI = Math.PI;
//getting coordinates of the vector
re = segment.X1()-segment.getX2();
im = segment.Y1()-segment.getY2();
//getting angle of the vector
if(im==0&&re<0) {
vectorAngle=Math.PI;
} else {
vectorAngle=2*Math.atan(im/(re+Math.sqrt(re*re+im*im)));
}
//deducting the basic rotation angle
basicRotationAngle = ((PI/2)*(1+Math.floor(vectorAngle/(PI/2))))-vectorAngle;
//changing orientation of the angle according to the segment position
rotationAngle = (im>=0&&re>=0) || (re<=0&&im<=0) ? (2*PI-basicRotationAngle):basicRotationAngle;
//applying complex rotation formula z'=(z-w)e^(iθ)+w around segment extremities (with the deduced cartesian form)
x1rot = (int) (rectangleWidth*Math.cos(rotationAngle )+segment.getX1()+rectangleWidth);
y1rot = (int) (rectangleWidth*Math.sin(rotationAngle )+segment.getY1());
x2rot = (int) (rectangleWidth*Math.cos(rotationAngle )+segment.getX2()+rectangleWidth);
y2rot = (int) (rectangleWidth*Math.sin(rotationAngle )+segment.getY2());
p.addPoint(segment.getX2(), segment.getY2());
p.addPoint(x2rot,y2rot);
p.addPoint(x1rot,y1rot);
p.addPoint(segment.getX1(), segment.getY1());
g.fillPolygon(p);
break;