gpt4 book ai didi

java - 增加距离后计算点的 x y

转载 作者:行者123 更新时间:2023-12-01 13:41:40 24 4
gpt4 key购买 nike

我试图在增加距离 r 后获取点的 x 和 y 值。也许还有更好的方法来计算角度 phi,这样我就不需要检查该点在哪个象限。 0 点位于窗口宽度和高度的一半处。这是我的尝试:

package test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public final class Laser extends java.applet.Applet implements Runnable{

private static final long serialVersionUID = -7566644836595581327L;

Thread runner;

int width = 800;
int height = 600;

Point point = new Point(405,100);
Point point1 = new Point(405,100);

public void calc(){

int x = getWidth()/2;
int y = getHeight()/2;
int px = point.x;
int py = point.y;
int px1 = point1.x;
int py1 = point1.y;
double r = 0;
double phi = 0;

// Point is in:
// Quadrant 1
if(px > x && py < y){
r = Math.hypot(px1-x, y-py1);
phi = Math.acos((px1-x)/r)*(180/Math.PI);
}/*
// Quadrant 2
else if(px < x && py < y){
r = Math.hypot(x-px, y-py);
phi = Math.acos((px-x)/r)*(180/Math.PI);
}
// Quadrant 3
else if(px < x && py > y){
r = Math.hypot(x-px, py-y);
phi = Math.acos((px-x)/r)*(180/Math.PI)+180;
}
// Quadrant 4
else if(px > x && py > y){
r = Math.hypot(px-x, py-y);
phi = Math.acos((px-x)/r)*(180/Math.PI)+180;
}*/
r += 1;
point1.x = (int) (r*Math.cos(phi));
point1.y = (int) (r*Math.sin(phi));
System.out.println(r+";"+point1.x+";"+point1.y);

}

public void paint(Graphics g) {

g.setColor(Color.ORANGE);
calc();
g.drawLine(point.x, point.y, point1.x, point1.y);

int h = getHeight();
int w = getWidth();
g.setColor(Color.GREEN);
g.drawLine(0, h/2, w, h/2);
g.drawLine(w/2, 0, w/2, h);

}
/*
public void initPoints(){

for(int i = 0; i < pointsStart.length; i++){
int x = (int)(Math.random()*getWidth());
int y = (int)(Math.random()*getHeight());
pointsStart[i] = pointsEnd[i] = new Point(x,y);
}

}
*/
public void start() {

if (runner == null) {

runner = new Thread(this);
setBackground(Color.black);
setSize(width, height);
//initPoints();
runner.start();

}

}

@SuppressWarnings("deprecation")
public void stop() {

if (runner != null) {

runner.stop();

runner = null;

}

}


public void run() {

while (true) {

repaint();

try { Thread.sleep(700); }

catch (InterruptedException e) { }

}

}

public void update(Graphics g) {

paint(g);

}

}

最佳答案

您正在将 (x,y) 从其他点更改为 r,而它之前距该点有一定距离 r',对吗?那么为什么不避免三角函数,而只是将每个分量从该点缩放 r/r' 呢?

编辑:好的,沿着较长的分量(x 或 y)迭代像素(假设它是 y);对于 (0..x) 中的每个 xi,yi = xi*(y/x),然后绘制 (xi,yi)。

关于java - 增加距离后计算点的 x y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20703901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com