所以我在仅使用线条绘制形状时遇到了一个大问题。假设我开始从屏幕中间的一点绘制一条线,并以 0 度的角度向前绘制 100 距离(以像素为单位),然后使用 72 度的角度绘制另一条相同长度的线,依此类推,直到 360 度。它应该给我完美的五边形,其中一条线结束,另一条线从该点开始,但线在末端不相交,它非常适合角度为 0/90/180/270 的正方形,但我需要让它适用于每个塑造均匀的圆圈。我正在使用这个东西进行计算:
_endingPointX = (_currentPostisionX + distance * _cosinuses[_angle]);
_endingPointY = (_currentPostisionY + distance * _sinuses[_angle]);
其中 _cosinuses 和 _sinuses 是 double 数组,其中包含 360 度每一度的正弦值和余弦值。当画一条线时,我需要将这些值转换为整数。
drawLine(_currentPostisionX, _currentPostisionY, (int) _endingPointX, (int) _endingPointY);
我不知道如何解决这个问题并使线条在绘制形状的末尾相交。几天来我一直试图解决这个问题,但我什么也没想到。
这是一个屏幕截图:
问题已解决,谢谢大家的建议,这是我使用整数转换的错误。
最佳答案
在绘制之前立即计算 double 和四舍五入的所有值。
不要用四舍五入进一步计算。
要绘制五边形或多边形,请使用类似于以下内容的内容:
// number of corners of pentagon
double numVertex = 5;
// how much to change the angle for the next corner( of the turtle )
double angleStep = 360.0 / numVertex;
gc.moveTo(cx, cy - rad);
for (int i= 1; i < numVertex; i++) {
// total angle from 0 degrees
double angle = i* angleStep;
// px point of turtle is corner of pentagon
double px = cx + rad * sin(angle * DEG_TO_RADIANS);
// move turtle to
gc.lineto((int)Math.round(px),
(int)Math.round(py));
}
gc.lineTo(cx, cy - rad);
如果您使用 lineTo
而不是线,点相交的机会会更高。
关于java - 在JAVA中用线条绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13906086/