第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使矩形與圓相切

如何使矩形與圓相切

蝴蝶刀刀 2023-06-04 17:36:02
我正在嘗試使一個矩形對象圍繞一個圓圈,矩形始終與它所圍繞的圓相切。我有讓它繞著圓圈轉的代碼,但我不知道如何讓它與它相切。到目前為止,這就是它的樣子。我正在使用動畫計時器,因為我不知道矩形將遵循的完整路徑,因為如果我發(fā)現有東西阻擋它,它可能會改變。我可以使矩形以平滑的方式繞圓,但我不知道如何使矩形與其相切。 public void moveInCircle(double radius)   {  double newX = getX() + (radius * Math.cos(Math.toDegrees(angle)));  double newY = getY() + (radius * Math.sin(Math.toDegrees(angle)));  vehicle.setTranslateX(newX);  vehicle.setTranslateY(newY);   }我知道切線是相鄰邊 (x) 除以對邊 (y),但我不知道如何合并它。
查看完整描述

1 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

我建議使用Rotate轉換。這樣您只需要設置初始位置和樞軸點,并且可以限制對Rotate.angle屬性的更新。


以下示例使用 aTimeline為屬性設置動畫,但這可以通過moveCircle使用 方法輕松完成rotate.setAngle(angleDegrees);:


@Override

public void start(Stage primaryStage) {

    Pane root = new Pane();

    root.setMinSize(500, 500);


    final double radius = 150;

    final double centerX = 250;

    final double centerY = 250;

    final double height = 40;


    Circle circle = new Circle(centerX, centerY, radius, null);

    circle.setStroke(Color.BLACK);


    // rect starts at the rightmost point of the circle touching it with the left midpoint

    Rectangle rect = new Rectangle(centerX + radius, centerY - height / 2, 10, height);

    rect.setFill(Color.RED);


    Rotate rotate = new Rotate(0, centerX, centerY); // pivot point matches center of circle


    rect.getTransforms().add(rotate);


    // animate one rotation per 5 sec 

    Timeline animation = new Timeline(

            new KeyFrame(Duration.ZERO, new KeyValue(rotate.angleProperty(), 0d)),

            new KeyFrame(Duration.seconds(5), new KeyValue(rotate.angleProperty(), 360d)));

    animation.setCycleCount(Animation.INDEFINITE);

    animation.play();


    root.getChildren().addAll(circle, rect);


    Scene scene = new Scene(root);

    primaryStage.setScene(scene);

    primaryStage.show();

}

順便說一句:您的代碼的以下部分看起來很奇怪


double newX = getX() + (radius * Math.cos(Math.toDegrees(angle)));

double newY = getY() + (radius * Math.sin(Math.toDegrees(angle)));

Math.sin并Math.cos期望弧度,而不是度數。您要么需要使用toRadians,要么不需要轉換...


查看完整回答
反對 回復 2023-06-04
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號