游戏开发论坛

 找回密码
 立即注册
搜索
查看: 5197|回复: 4

?删Q段相交??取

[复制链接]

1万

主题

1万

帖子

2万

积分

管理员

中级会员

Rank: 9Rank: 9Rank: 9

积分
20737
发表于 2004-2-10 10:32:00 | 显示全部楼层 |阅读模式
//代码名称: ?删Q段相交??取
//类别:       ??W算法
//关健字:    交?、相交
//作者:       林德?(Sea_Bug)
//编译器:    VC, others
//操作系统: Windows, others

struct tag_msLine
{
    float fBeginX, fBeginY;
    float fEndX, fEndY;
    float fK;
};
typedef tag_msLine* LP_MS_LINE;
typedef tag_msLine MS_LINE;


// 交换两个浮点数,大->小排列
void msMax2Min( float* fA, float* fB )
{
    if( *fA < *fB )
    {
        float temp = *fA;
        *fA = *fB;
        *fB = temp;
    }
}


// 获取相交的坐标点
// 两条线段Line1 & Line2 计算其交点,并返回为参数指针x,y
// 若没有交点,函式返回false
bool msGetX( LP_MS_LINE pLine1, LP_MS_LINE pLine2, float* x, float* y )
{
    // 获取两线交点
    *x = ( pLine2->fK * pLine2->fBeginX - pLine1->fK * pLine1->fBeginX + pLine1->fBeginY - pLine2->fBeginY ) / ( pLine2->fK - pLine1->fK );
    *y = pLine1->fK * ( *x - pLine1->fBeginX ) + pLine1->fBeginY;

    // 坐标排序,以便于后面计算
    MS_LINE Line1, Line2;
    Line1.fBeginX = pLine1->fBeginX;
    Line1.fBeginY = pLine1->fBeginY;
    Line1.fEndX = pLine1->fEndX;
    Line1.fEndY = pLine1->fEndY;

    Line2.fBeginX = pLine2->fBeginX;
    Line2.fBeginY = pLine2->fBeginY;
    Line2.fEndX = pLine2->fEndX;
    Line2.fEndY = pLine2->fEndY;

    msMax2Min( &Line1.fEndX, &Line1.fBeginX );
    msMax2Min( &Line1.fEndY, &Line1.fBeginY );
    msMax2Min( &Line2.fEndX, &Line2.fBeginX );
    msMax2Min( &Line2.fEndY, &Line2.fBeginY );


    // 判定交点是否在线段内
    if( *x > Line1.fBeginX && *x < Line1.fEndX && *y > Line1.fBeginY && *y < Line1.fEndY )
        if( *x > Line2.fBeginX && *x < Line2.fEndX && *y > Line2.fBeginY && *y < Line2.fEndY )
            return true;
    return false;
}

1

主题

40

帖子

48

积分

注册会员

Rank: 2

积分
48
发表于 2004-5-13 17:42:00 | 显示全部楼层

Re:?删Q段相交??取

我靠没人顶啊,太失败了,顶

1

主题

10

帖子

10

积分

新手上路

Rank: 1

积分
10
发表于 2004-7-7 23:21:00 | 显示全部楼层

Re:?删Q段相交??取

鼓励

0

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2004-9-17 13:31:00 | 显示全部楼层

Re:?删Q段相交??取

鼓励

11

主题

218

帖子

328

积分

中级会员

Rank: 3Rank: 3

积分
328
发表于 2007-5-15 21:17:00 | 显示全部楼层

Re:?删Q段相交??取

我顶~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-26 10:41

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表