增加距离后计算点的x y

时间:2013-12-20 12:43:20

标签: java point

我试图在增加距离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);

}

}

2 个答案:

答案 0 :(得分:1)

你正在将(x,y)从某个其他点改变为r,当它之前离那个点有一段距离r'时,对吗?那么为什么不避免三角学,只需通过r / r'?

从那一点开始扩展每个组件

编辑:好的,沿着任何一个组件(x或y)更长的像素迭代(让我们假设它是y);对于(0..x)中的每个xi,yi = xi *(y / x),并绘制(xi,yi)。

答案 1 :(得分:0)

保持简单!并使用double变量进行此类计算,我为您更改了它。

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);

    }

}