Сколько точек пересечения имеют окружность и луч?

2 ответов на вопрос “Сколько точек пересечения имеют окружность и луч?”

  1. Dirk Ответить

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    #include
    #include
    using namespace std;
    static const float Epsilon = 1e-4f;
    struct Vector3F
    {
    public:
    float x;
    float y;
    float z;
    };
    struct Intersection
    {
    public:
    Vector3F p0;
    Vector3F p1;
    unsigned char count;
    };
    inline float operator*(Vector3F p0, Vector3F p1)
    {
    return p0.x*p1.x + p0.y*p1.y + p0.z*p1.z;
    }
    inline Vector3F operator*(Vector3F p0, float t)
    {
    p0.x *= t;
    p0.y *= t;
    p0.z *= t;
    return p0;
    }
    inline Vector3F operator-(Vector3F p0, Vector3F p1)
    {
    p0.x -= p1.x;
    p0.y -= p1.y;
    p0.z -= p1.z;
    return p0;
    }
    Intersection FindIntersection(
    Vector3F rayStart,
    Vector3F rayDirection,
    Vector3F sphereCenter,
    float sphereRadius)
    {
    Vector3F delta;
    delta = sphereCenter – rayStart;
    float a = rayDirection*rayDirection;
    float half_b = rayDirection*delta*(-1.0f);
    float c = delta*delta – sphereRadius*sphereRadius;
    Intersection otvet;
    //assert(fabs(a) > Epsilon);//lineynoye uravneniye //luch raven nulyu ne mozhet
    float Discr = half_b*half_b – a*c;
    if (Discr < -Epsilon) {// 0 korney otvet.count = 0; return otvet; } if (Discr > Epsilon)
    {// 2 korney
    otvet.count = 2;
    float minus_t0 = (half_b + sqrt(Discr) )/a;
    float minus_t1 = (half_b – sqrt(Discr) ) / a;
    otvet.p0 = rayStart – rayDirection*minus_t0;
    otvet.p1 = rayStart – rayDirection*minus_t1;
    return otvet;
    }
    {// 1 korney
    otvet.count = 1;
    float minus_t0 = half_b/ a;
    otvet.p0 = rayStart – rayDirection*minus_t0;
    return otvet;
    }
    }
    int main() {
    const Vector3F rayStart{ 0.0f, 0.0f, 4.0f };
    const Vector3F rayDirection{ 0.0f, 0.0f, -1.0f };
    const Vector3F sphereCenter{ 0.0f, 0.0f, 0.0f };
    const float sphereRadius = 1.0f;
    const Intersection result = FindIntersection(
    rayStart,
    rayDirection,
    sphereCenter,
    sphereRadius);
    cout < < "Intersection test result: " << endl; cout << "P0 = (" << result.p0.x << ", " << result.p0.y << ", " << result.p0.z << ")" << endl; cout << "P1 = (" << result.p1.x << ", " << result.p1.y << ", " << result.p1.z << ")" << endl; cout << "Count= " << static_cast(result.count) << endl; system("pause"); return 0; }

  2. VideoAnswer Ответить

Добавить ответ

Ваш e-mail не будет опубликован. Обязательные поля помечены *