Java 增加距离后计算点的x y
我试图在增加距离r后得到点的x和y值。也许还有更好的方法来计算角度φ,这样我就不需要检查点在哪个象限。0点位于窗口宽度和高度的一半。以下是我的尝试:Java 增加距离后计算点的x y,java,point,Java,Point,我试图在增加距离r后得到点的x和y值。也许还有更好的方法来计算角度φ,这样我就不需要检查点在哪个象限。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 stati
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);
}
}
封装测试;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Point;
公共最终类Laser扩展java.applet.applet实现可运行{
私有静态最终长serialVersionUID=-7566644836595581327L;
螺纹流道;
整数宽度=800;
内部高度=600;
点=新点(405100);
点1=新点(405100);
公共无效计算(){
int x=getWidth()/2;
int y=getHeight()/2;
int px=点x;
int py=点y;
int px1=point1.x;
int py1=点1.y;
双r=0;
双φ=0;
//重点在于:
//象限1
如果(px>x&&pyy){
r=数学形下压(x-px,py-y);
φ=数学acos((px-x)/r)*(180/数学PI)+180;
}
//象限4
else if(px>x&&py>y){
r=数学形下压(px-x,py-y);
φ=数学acos((px-x)/r)*(180/数学PI)+180;
}*/
r+=1;
点1.x=(int)(r*Math.cos(phi));
点1.y=(int)(r*Math.sin(phi));
System.out.println(r+“;”+point1.x+“;”+point1.y);
}
公共空间涂料(图g){
g、 setColor(颜色为橙色);
计算();
g、 抽绳(点x、点y、点1.x、点1.y);
inth=getHeight();
int w=getWidth();
g、 setColor(Color.GREEN);
g、 抽绳(0,h/2,w,h/2);
g、 抽绳(w/2,0,w/2,h);
}
/*
公共点(){
for(int i=0;i
您正在将(x,y)从某个其他点更改为r,而之前距离该点的距离为r',对吗?那么,为什么不避开三角法,从该点开始,用r/r'缩放每个分量呢
编辑:好的,沿着哪个分量(x或y)迭代像素(假设它是y);对于每个席席(0…x),Y==X*(y/x),和你的情节(席,彝)。 < P> >保持简单!并使用双变量进行此类计算,我为您更改了它
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Point2D;
public final class Laser extends java.applet.Applet implements Runnable {
private static final long serialVersionUID = -7566644836595581327L;
Thread runner;
int width = 800;
int height = 600;
Point2D.Double point = new Point2D.Double(400, 100);
Point2D.Double point1 = new Point2D.Double(405, 102);
public void calc() {
double px = point.x;
double py = point.y;
double px1 = point1.x;
double py1 = point1.y;
double dx = px1 - px;
double dy = py1 - py;
double len = Math.hypot(dx, dy);
double newlen = len+2;
double coeff = Math.abs((newlen-len)/len);
point1.x += dx * coeff;
point1.y += dy * coeff;
System.out.println(len+";"+point1.x+";"+point1.y);
}
public void paint(Graphics g) {
g.setColor(Color.ORANGE);
calc();
g.drawLine((int)point.x, (int)point.y, (int)point1.x, (int)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(double i = 0; i < pointsStart.length; i++){ double x =
* (double)(Math.random()*getWidth()); double y =
* (double)(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);
}
}
封装测试;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.geom.Point2D;
公共最终类Laser扩展java.applet.applet实现可运行{
私有静态最终长serialVersionUID=-7566644836595581327L;
螺纹流道;
整数宽度=800;
内部高度=600;
Point2D.Double point=新的Point2D.Double(400100);
Point2D.Double point1=新的Point2D.Double(405102);
公共无效计算(){
双px=点x;
双py=点y;
双px1=点1.x;
双py1=点1.y;
双dx=px1-px;
双dy=py1-py;
双透镜=数学形波(dx,dy);
双纽伦=伦+2;
双系数=数学绝对值((newlen-len)/len);
点1.x+=dx*系数;
点1.y+=dy*系数;
System.out.println(len+“;”+point1.x+“;”+point1.y);
}
公共空间涂料(图g){
g、 setColor(颜色为橙色);
计算();
g、 抽绳((int)点x,(int)点y,(int)点1.x,(int)点1.y);
inth=getHeight();
int w=getWidth();
g、 setColor(Color.GREEN);
g、 抽绳(0,h/2,w,h/2);
g、 抽绳(w/2,0,w/2,h);
}
/*
*公共点(){
*
*对于(double i=0;i
您的尝试有效吗?我不确定你的问题到底是什么。Math.atan2返回-pi到pi范围内的一个角度,用于笛卡尔坐标到极坐标的转换。我暂时在这里上传了一张图片:[链接]。我想从随机生成的红点开始,逐像素绘制绿线。请尝试这一条:,如果你需要通过它:pppppp