我正在尝试使用JavaFX绘制转弯的赛道。
我通过在我的GraphicsContext上绘制带有路径的形状来划定车道。
问题是我永远不需要使arc正常工作,所以我使用的弧总是根据我的理解总是逆时针绘制,这使我的转弯变得困难,这是我得到的最佳结果:
innerRadius
是中心与(x1,y1)之间的距离,而radius
是中心与(x0,y0)之间的距离。
这是我的代码:
gc.beginPath();
gc.moveTo(x1, y1);
gc.lineTo(x0, y0);
gc.arc(centerX, centerY, radius, radius, startAngle, arcLength);
gc.lineTo(x3, y3);
gc.arc(centerX, centerY, innerRadius, innerRadius, startAngle, arcLength);
gc.closePath();
我不知道如何使路径不包括从(x2,y2)到(x1,y1)的线。 我宁愿坚持使用arc,但是如果您知道如何做arc要使用我拥有的变量,请继续。
谢谢。
答案 0 :(得分:0)
问题出在这一行:
gc.arc(centerX, centerY, innerRadius, innerRadius, startAngle, arcLength);
此处起始角度为“向下”,并且应沿绘制外部圆弧的方式逆时针绘制圆弧。尽管您需要为圆弧使用不同的参数,但您想沿相反的方向绘制圆弧:
gc.arc(centerX, centerY, innerRadius, innerRadius, startAngle + arcLength, -arcLength);
您还可以摆脱lineTo
之一,因为closePath
自动建立到路径起点的连接。
@Override
public void start(Stage stage) {
Canvas canvas = new Canvas(400, 400);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.BLACK);
gc.setFill(Color.LIGHTGRAY);
final double centerX = 100;
final double centerY = 200;
final double dR = 20;
final double radius = (350 - centerY) / 2;
final double innerRadius = radius - dR;
final double x0 = centerX;
final double y0 = centerY + radius;
final double x3 = x0 + innerRadius;
final double y3 = centerY;
final double startAngle = 270;
final double arcLength = 90;
gc.beginPath();
gc.moveTo(x0, y0);
gc.arc(centerX, centerY, radius, radius, startAngle, arcLength);
gc.lineTo(x3, y3);
gc.arc(centerX, centerY, innerRadius, innerRadius, startAngle + arcLength, -arcLength);
gc.closePath();
gc.fill();
gc.stroke();
Scene scene = new Scene(new StackPane(canvas));
stage.setScene(scene);
stage.show();
}