0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C# メモ 任意の3点を通る平面と2点間を通る直線、その交点を求めるのに使ったのを残すためのメモ。

Last updated at Posted at 2020-01-20

実際にはPlaneとRayだけでやれる。
軸が違うので変換or変数入れ替えしないといけない。
(このコードでは高さがz奥行きがy、幅がx,unityでは高さy,奥行きz,幅x向きも違う。)


using UnityEngine;

public class StraightLine
{
    public readonly float XParamCoefficient;
    public readonly float XConstant;
    public readonly float YParamCoefficient;
    public readonly float YConstant;
    public readonly float ZParamCoefficient;
    public readonly float ZConstant;

    public StraightLine(float XParamCoefficient, float XConstant,
                        float YParamCoefficient, float YConstant,
                        float ZParamCoefficient, float ZConstant)
    {
        this.XParamCoefficient = XParamCoefficient;
        this.XConstant = XConstant;
        this.YParamCoefficient = YParamCoefficient;
        this.YConstant = YConstant;
        this.ZParamCoefficient = ZParamCoefficient;
        this.ZConstant = ZConstant;
    }

    public static StraightLine Make(Vector3 p1, Vector3 p2)
    {
        var d = p2 - p1;
        return new StraightLine(d.x, p1.x, d.y, p1.y, d.z, p1.z);
    }
}
using UnityEngine;

public sealed class Plane
{
    private readonly float p;
    private readonly float q;
    private readonly float r;

    private readonly float x0;
    private readonly float y0;
    private readonly float z0;

    private Plane(float p, float q, float r, float x0, float y0, float z0)
    {
        this.p = p;
        this.q = q;
        this.r = r;
        this.x0 = x0;
        this.y0 = y0;
        this.z0 = z0;
    }

    public static Plane Make(Vector3 a, Vector3 b, Vector3 c)
    {
        var ab = b - a;
        var ac = c - a;
        var n = new Vector3(ab.y * ac.z - ab.z * ac.y,
                            ab.z * ac.x - ab.x * ac.z,
                            ab.x * ac.y - ab.y * ac.x);

        return new Plane(n.x, n.y, n.z, a.x, a.y, a.z);
    }

    public Vector3 GetIntersection(float XParamCoefficient, float XConstant,
                                   float YParamCoefficient, float YConstant,
                                   float ZParamCoefficient, float ZConstant)
    {
        var param = (p * (x0 + XConstant) + q * (y0 + YConstant) + r * (z0 + ZConstant))
                    / (p * XParamCoefficient + q * YParamCoefficient + r * ZParamCoefficient);

        return new Vector3(XParamCoefficient * param - XConstant,
                           YParamCoefficient * param - YConstant,
                           ZParamCoefficient * param - ZConstant);
    }
}
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?