2012-08-14 34 views
1

Image我如何計算相反點,如果我有一個點和一箇中心?

我有一箇中心點,我知道點1,現在我想計算點2在相反的方向,但有另一個長度。另外我知道從中心點到第2點的長度,但它與中心點1不在同一個矢量上。想象一下虛線與圖像顯示的另一個角度。

點2應與點1位於相同的矢量上。換句話說,點1到點2應該是一條穿過中心的直線。

我希望有人能幫助我。

非常感謝您提前。

+0

'POINT2 =中心+ distance_from_center *歸一化(中心 - 點1);' – Bart 2012-08-14 22:14:56

+0

由於這是一個非常數學定向問題我需要一些澄清: 這是在2D平面,如果是笛卡爾座標或極座標?如果在3D空間中使用矢量? – 2012-08-14 22:22:39

+0

它在2D平面。 不知道什麼是極座標 – 2012-08-14 23:00:08

回答

3

這將工作:

代碼假定共線點,在二維平面上,定義爲直角座標

在Java:

class GoGo { 

    public static void main (String[] args) { 
    double[] center = new double[] { 4.0,3.0 }; 
    double[] point1 = new double[] { 8.0,4.0 }; 
    double[] point2 = getPoint2(center,point1,4.0); 
    System.out.print("X: " + point2[0] + " Y: " +point2[1]); 
    } 
    public static double[] getPoint2(double[] center, double[] point1, double distance) { 
    //assumes Points = double[2] { xValue, yValue } 

    double[] point2 = new double[2]; 
    double changeInX = point1[0] - center[0]; // get delta x 
    double changeInY = point1[1] - center[1]; // get delta y 
    double distanceCto1 = Math.pow(    // get distance Center to point1 
          (Math.pow(changeInX,2.0) + Math.pow(changeInY,2.0)) 
          ,0.5); 
    double distanceRatio = distance/distanceCto1;// ratio between distances 
    double xValue = distanceRatio * changeInX; // proportional change in x 
    double yValue = distanceRatio * changeInY; // proportional change in y 
    point2[0] = center[0] - xValue;    // normalize from center 
    point2[1] = center[0] - yValue;    // normalize from center 
    return point2;        // and return 
    } 
} 

我寫這在Java中,因爲它是我的首選語言,您沒有指定您需要答案的語言。如果您有不同的語言偏好,我可以嘗試將代碼移植到首選語言(假設我知道它)。

CODE給出:馬塞羅Stanisci

在Objective C:

- (CGPoint) getOppositePointAtCenter2:(CGPoint)center fromPoint:(CGPoint)point oppositeDistance:(double)oppositeDistance { 

    CGPoint vector = CGPointMake(point.x - center.x, point.y - center.y); 
    double distanceCenterToPoint1 = pow(pow(vector.x, 2) + pow(vector.y, 2), 0.5); 
    double distanceRatio = oppositeDistance/distanceCenterToPoint1; 

    double xValue = distanceRatio * vector.x; 
    double yValue = distanceRatio * vector.y; 

    return CGPointMake(center.x - xValue, center.y - yValue); 
} 
+0

我在將Java代碼轉換爲目標c時犯了一個錯誤。 現在我已經糾正它,它的作品就像一個魅力。 非常感謝你。 – 2012-08-15 01:04:24

+0

@MarcelloStanisci我剛剛編輯了這篇文章,如果你想重新編輯修改後的Objective C代碼的帖子,這對未來的觀衆很有用。也可能接受答案... – 2012-08-15 01:09:17

+0

我糾正了我的代碼,也許我不得不等待一點點,只是它是可見的。 我現在如何接受答案? – 2012-08-15 01:14:30

相關問題