| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3394 人关注过本帖
标题:如何用C语言解出多条线段的交点坐标?求大师赐教!
只看楼主 加入收藏
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
你始終都是在看別人的代碼,不是自己寫的。

授人以渔,不授人以鱼。
2014-05-11 11:28
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
收藏
得分:0 
以下是引用宇宙规律在2014-5-10 15:21:05的发言:

金融市场的非线性混沌很难预测准确的原因,可能是数学哲学道理落后了!
 
例如:天道盈而不溢,胜而不骄,劳而不持其功;等;哲学落后=问题定性模糊,结果定量后预测经常失误!
果然,楼主讲的都是宇宙的真理,宇宙的规律呀!
2014-05-11 11:32
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
祗是給個思路:

程序代码:
#include "stdafx.h"

using namespace System;

// 點類定義
struct Point
{
    Double x;
    Double y;

    Point() : x(Double::NaN), y(Double::NaN) {}
    Point(Double x, Double y) : x(x), y(y) {}
};

// 直線類定義
class Line sealed
{
private:

    Double slope;        // 斜率
    Double ordinate;    // 截距

    Double x;
    Double y;

public:

    Line() : slope(Double::NaN), ordinate(Double::NaN) {}

    // 用兩點式確定直線
    Line(Point point1, Point point2)
    {
        if (point1.x != point2.x)
        {
            slope = (point1.y - point2.y) / (point1.x - point2.x);
            ordinate = (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
        }
        else
        {
            slope = Double::NaN;
            ordinate = Double::NaN;
            x = point1.x;
        }
    }

    Double Get_Y(Double x) { return !Double::IsNaN(slope) ? slope * x + ordinate : Double::NaN; }
    Double Get_X(Double y) { return !Double::IsNaN(slope) ? (y - ordinate) / slope : y; }

    // 判斷與另一直線是否重合
    Boolean Equation(Line line)
    {
        if (!Double::IsNaN(slope) && !Double::IsNaN(line.slope) && (slope == line.ordinate) && (ordinate == line.ordinate))
        {
            return true;
        }

        if (Double::IsNaN(slope) && Double::IsNaN(line.slope) && x == line.x)
        {
            return true;
        }

        return false;
    }

    // 判斷與另一直線是否平行
    Boolean Parallel(Line line)
    {
        if (!Double::IsNaN(slope) && !Double::IsNaN(line.slope) && (slope == line.ordinate) && (ordinate != line.ordinate))
        {
            return true;
        }

        if (Double::IsNaN(slope) && Double::IsNaN(line.slope) && x != line.x)
        {
            return true;
        }

        return false;
    }

    // 求與指定直線的交點
    Point Get_Intersection(Line line)
    {
        Point intersection;

        if (!Equation(line) && !Parallel(line))
        {
            // 分是否存在垂直(斜率無線大)線兩種情形處理
            if (!Double::IsNaN(slope) && !Double::IsNaN(line.slope))
            {
                intersection.x = (line.ordinate - ordinate) / (slope - line.slope);
                intersection.y = (slope * line.ordinate - line.slope * ordinate) / (slope - line.slope);
            }
            else
            {
                if (Double::IsNaN(slope))
                {
                    intersection.x = x;
                    intersection.y = line.Get_Y(intersection.x);
                }
                else
                {
                    intersection.x = line.x;
                    intersection.y = Get_Y(intersection.x);
                }
            }
        }

        return intersection;
    }
};

int main(array<String ^> ^args)
{
    Line line1(Point{ 1.0, 2.0 }, Point{ 2.0, 5.0 });
    Line line2(Point{ 1.0, 1.0 }, Point{ 3.0, 3.0 });
    Point intersection = line1.Get_Intersection(line2);
    Console::WriteLine(L"交點:({0:F2},{1:F2})", intersection.x, intersection.y);
    return 0;
}

求出直線的交點,然後判斷它是否在所給端點的之間(分別投影到x軸和y軸上判斷),即可知道線段是否有交點。

[ 本帖最后由 TonyDeng 于 2014-5-12 22:40 编辑 ]

授人以渔,不授人以鱼。
2014-05-12 21:58
快速回复:如何用C语言解出多条线段的交点坐标?求大师赐教!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019897 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved