LoginSignup
1
2

More than 5 years have passed since last update.

2点間の距離、中点の座標を求める

Posted at

数学公式

平面上の2点 A(x1, y1), B(x2, y2) 間の距離は、
$$ AB = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$
中点の座標は、
$$ \left(\frac{x_1 + x_2}{2},\frac{y_1 + y_2}{2} \right)$$


空間の2点 A(x1, y1, z1), B(x2, y2, z2) 間の距離は、
$$ AB = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2}$$
中点の座標は、
$$ \left(\frac{x_1 + x_2}{2},\frac{y_1 + y_2}{2},\frac{z_1 + z_2}{2} \right)$$


AutoLISPで求める

距離は distance 関数で求める。

;; 2点の中点を計算
(defun point:GetMidpoint (a b)
    (mapcar (function (lambda (a b) (/ (+ a b) 2.))) a b)
)
;; < Example >
;; 2D
(distance '(4 1) '(-1 4))
;; -> 5.83095
(point:GetMidpoint '(4 1) '(-1 4))
;; -> (1.5 2.5)

;; 3D
(distance '(-5 1 2) '(4 -1 -4))
;; -> 11
(point:GetMidpoint '(-5 1 2) '(4 -1 -4))
;; -> (-0.5 0.0 -1.0)

AutoCAD .NET(C#)で求める

2D点はPoint2d構造体、3D点はPoint3d構造体を使う。
距離は GetDistanceTo メソッドで求める。

// 2点の中点を計算(2D)
public static Point2d GetMidpoint2d(Point2d a, Point2d b) {
    return new Point2d((a.X + b.X) / 2, (a.Y + b.Y) / 2);
}

// 2点の中点を計算(3D)
public static Point3d GetMidpoint3d(Point3d a, Point3d b) {
     return new Point3d((a.X + b.X) / 2, (a.Y + b.Y) / 2, (a.Z + b.Z) / 2);
}

// < Example >
// 2D
var pt2d1 = new Point2d(4, 1);
var pt2d2 = new Point2d(-1, 4);
pt2d1.GetDistanceTo(pt2d2);
// -> 5.83095
GetMidpoint2d(pt2d1, pt2d2);
// -> (1.5, 2.5)

// 3D
var pt3d1 = new Point3d(-5, 1, 2);
var pt3d2 = new Point3d(4, -1, -4);
pt3d1.GetDistanceTo(pt3d2);
// -> 11
GetMidpoint3d(pt3d1, pt3d2);
// -> (-0.5, 0.0, -1.0)

ObjectARX(C++)で求める

2D点はAcGePoint2dクラス、3D点はAcGePoint3dクラスを使う。
距離は distanceTo メソッドで求める。

// 2点の中点を求める (2D)
satic AcGePoint2d getMidpoint2d(AcGePoint2d a, AcGePoint2d b) {
    return AcGePoint2d((a.x + b.x) / 2, (a.y + b.y) / 2);
}

// 2点の中点を求める (3D)
static AcGePoint3d getMidpoint3d(AcGePoint3d a, AcGePoint3d b) {
    return AcGePoint3d((a.x + b.x) / 2, (a.y + b.y) / 2, (a.z + b.z) / 2);
}

// < Example >
// 2D
auto pt2d1 = AcGePoint2d(4, 1);
auto pt2d2 = AcGePoint2d(-1, 4);
pt2d1.distanceTo(pt2d2);
// -> 5.83095
getMidpoint2d(pt2d1, pt2d2);
// -> (1.5, 2.5)

// 3D
auto pt3d1 = AcGePoint3d(-5, 1, 2);
auto pt3d2 = AcGePoint3d(4, -1, -4);
pt3d1.distanceTo(pt3d2);
// -> 11
getMidpoint3d(pt3d1, pt3d2);
// -> (-0.5, 0.0, -1.0)
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2