資源簡介
C++語言實現一些基本算法(兩點距離、點是否在直線上、點與直線的關系、兩直線的夾角、兩直線的交點、兩個舉行的重合面積等等)

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
#define?PI?3.141592653
struct?QUICKSORT
{
????int?iIndex;
????float?fAngle;
????QUICKSORT(int?iIndex?float?fAngle)
????{
????????this->iIndex?=?iIndex;
????????this->fAngle?=?fAngle;
????}
????QUICKSORT()?{memset(this?0?sizeof(*this));}
};
//定義點結構體
struct?myPoint
{
????int?x;?//點的x坐標
????int?y;?//點的y坐標
????myPoint(){memset(this?0?sizeof(*this));}
????myPoint(int?x?int?y)
????{
????????this->x?=?x;
????????this->y?=?y;
????}
????bool?operator==(const?myPoint?&other)?const
????{
????????return?(this->x?==?other.x?&&?this->y?==?other.y);
????}
};
/********************************************************************
?*?函數名稱?:?segmentLength
?*?函數功能?:?求兩點的長度(勾股定理)
?*?輸入參數1:?pt1?第一個點
?*?輸入參數2:?pt2?第二個點
?*?輸出參數:?無
?*?返回參數:?pt1到pt2的距離
?*?日???期:?2018年09月13日
?*?作???者:?mark-plus
********************************************************************/
float?segmentLength(const?myPoint?&pt1?const?myPoint?&pt2)
{
????float?x?=?fabs(pt1.x?-?pt2.x);
????float?y?=?fabs(pt1.y?-?pt2.y);
????float?fLength?=?sqrt((x?*?x?+?y?*?y));
????return?fLength;
}
/********************************************************************
?*?函數名稱?:?pointOnSegment
?*?函數功能?:?判斷點是否在線段上
?*?輸入參數1:?pt1?線段的第一個點
?*?輸入參數2:?pt2?線段的第二個點
?*?輸入參數3:?ptNode 需要判斷的點
?*?輸出參數:?無
?*?返回參數:?true:點ptNode在線段上 false:點ptNode在在線段上
?*?日???期:?2018年09月13日
?*?作???者:?mark-plus
********************************************************************/
bool?pointOnSegment(const?myPoint?&pt1?const?myPoint?&pt2?const?myPoint?&ptNode)
{
????float?a?=?segmentLength(pt1?pt2);
????float?b=?segmentLength(pt1?ptNode);
????float?c=?segmentLength(pt2?ptNode);
????if?(a?==?(b?+c))?return?true;
????else?return?false;
}
/********************************************************************
?*?函數名稱?:?pointOnSegmentOutSide
?*?函數功能?:?判斷某個點是否在線段外
?*?輸入參數1:?pt1?線段的第一個點
?*?輸入參數2:?pt2?線段的第二個點
?*?輸入參數3:?ptNode 需要判斷的點
?*?輸出參數:?無
?*?返回參數:?true:點ptNode在線外 false:點ptNode在線上
?*?日???期:?2018年09月13日
?*?作???者:?mark-plus
********************************************************************/
bool?pointOnSegmentOutSide(const?myPoint?pt1?const?myPoint?pt2?const?myPoint?ptNode)
{
????float?a?=?segmentLength(pt1?pt2);
????float?b=?segmentLength(pt1?ptNode);
????float?c=?segmentLength(pt2?ptNode);
????if?(b?>?a?||?c?>?a)?return?false;
????else?return?true;
}
/********************************************************************
?*?函數名稱?:?intersect
?*?函數功能?:?求兩直線的交點(參考了Qt里面兩直線求交點的算法)
?*?輸入參數1:?pt1 直線1的第1個點
?*?輸入參數2:?pt2 直線1的第2點
?*?輸入參數3:?pt3 直線2的第1個點
?*?輸入參數4:?pt4 直線2的第2個點
?*?輸出參數:?ptNode 直線1和直線2的交點
?*?返回參數:?true:直線1和直線2有交點 false:true:直線1和直線2沒有交點
?*?日???期:?2018年09月13日
?*?作???者:?mark-plus
********************************************************************/
bool?intersect(const?myPoint?&pt1?const?myPoint?&pt2?const?myPoint?&pt3?const?myPoint?&pt4?myPoint?&ptNode)
{
????myPoint?a(pt2.x?-?pt1.x?pt2.y?-?pt1.y);
????myPoin
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-09-14?17:47??algorithm\
?????文件??????????26??2018-09-14?14:29??algorithm\algorithm.pro
?????文件???????14177??2018-09-14?17:47??algorithm\algorithm.pro.user
?????文件???????24956??2018-09-14?16:13??algorithm\main.cpp
- 上一篇:C++編寫的類似水果忍者的切水果的效果
- 下一篇:C語言模糊控制程序
評論
共有 條評論