开发者

draw the two lines with intersect each other and need to find the intersect point in c# using directx? [closed]

开发者 https://www.devze.com 2022-12-16 21:49 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, 开发者_开发问答overly broad, or rhetorical andcannot be reasonably answered in its current form.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, 开发者_开发问答overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

draw the two lines with intersect each other and need to find the intersect point in c# using directx?


I can't remember much about direct3d at the moment; this website should get you started: http://www.drunkenhyena.com/cgi-bin/view_net_article.pl?chapter=2;article=21

As far as intersecting lines go, the following function should calculate a) whether or not two lines intersect at all, and if so where abouts.

static Point lineIntersect(Point a1, Point a2, Point b1, Point b2)
{
    float dx, dy, da, db, t, s;

    dx = a2.X - a1.X;
    dy = a2.Y - a1.Y;
    da = b2.X - b1.X;
    db = b2.Y - b1.Y;

    if (da * dy - db * dx == 0) {
        // The segments are parallel.
        return Point.Empty;
    }

    s = (dx * (b1.Y - a1.Y) + dy * (a1.X - b1.X)) / (da * dy - db * dx);
    t = (da * (a1.Y - b1.Y) + db * (b1.X - a1.X)) / (db * dx - da * dy);

    if ((s >= 0) & (s <= 1) & (t >= 0) & (t <= 1)) 
        return new Point((int)(a1.X + t * dx), (int)(a1.Y + t * dy));
    else
        return Point.Empty;
}

You didn't specify whether your lines are expressed as coordinates (2 coordinates per line) or whether its an equation so I've assumed you have 2 points.

In addition, I have assumed the lines are not infinitely long, and hence may not intersect because either they are parallel, or simply not long enough to intersect.

Finally, these are for 2d lines only, if you want the equivalent in 3d, you need to ask about intersecting planes

0

精彩评论

暂无评论...
验证码 换一张
取 消