Deutsch
Germany.ruФорумы → Архив Досок→ Программирование

Задачки любителям 3Д

25.06.21 12:38
Re: Задачки любителям 3Д
 
NightWatch коренной житель
NightWatch
в ответ AlexNek 25.06.21 12:12, Последний раз изменено 25.06.21 12:42 (NightWatch)
float distance = ...;
List<Line> lines = new List<Line>{...};
foreach (Line l in lines) {
    Line _1stGenLine = getGenLine(l, distance, 0.0f);
    Line _2ndGenLine = getGenLine(l, distance, (float)Math.PI);

    drawLine(l);
    drawLine(_1stGenLine);
    drawLine(_2ndGenLine);
}

private static Line getGenLine(Line line, float distance, float angle)
{
    Vector3 pnt1 = line.Point1;
    Vector3 pnt2 = line.Point2;

    Vector3 ray = pnt2 - pnt1;

    Matrix4x4 rotate1 = Matrix4x4.CreateRotationX(angle);
    Matrix4x4 rotate2 = getRotateMatrix(Vector3.UnitX, Vector3.Normalize(ray));
    Matrix4x4 rotate = Matrix4x4.Multiply(rotate1, rotate2);

    Vector3 axis = Vector3.Transform(Vector3.UnitY, rotate);

    Matrix4x4 translate = Matrix4x4.CreateTranslation(distance * axis);
    pnt1 = Vector3.Transform(pnt1, translate);
    pnt2 = Vector3.Transform(pnt2, translate);

    return new Line { Point1 = pnt1, Point2 = pnt2 };
}

private static Matrix4x4 getRotateMatrix(Vector3 v1, Vector3 v2)
{
    Vector3 axis = Vector3.Normalize(Vector3.Cross(v1, v2));
    float dotProduct = Vector3.Dot(v1, v2);
    dotProduct = Math.Max(
        -1.0f,
        Math.Min(1.0f, dotProduct)
    );
    float angleRadians = (float)Math.Acos(dotProduct);
    return Matrix4x4.CreateFromAxisAngle(axis, angleRadians);
}

private class Line
{
    public Vector3 Point1;
    public Vector3 Point2;
}
 

Перейти на