这几天把点和直线相关的几何知识学习了一下,把代码保存到这里以后需要使用的时候做个参考。如果有童鞋使用相关的函数,我不保证其正确性,因为该函数都未经过严格的测试。废话不多说,直接贴代码。
用到的头文件。
/** 定义类型 POINT */
typedef struct POINT
{
float x;/**< x轴上的点 */
float y;/**< y轴上的点 */
}Point;
/** 斜率无限大返回的值 */
#define SLOPE_MAX 999999
/** 两个浮点数比较定义差值 */
#define EPSINON 0.0001
/** 角度向弧度转换 */
#define ANGLE_TO_RADIAN 0.017453293f
/** 两个浮点数相等 */
#define EQUAL 1
/** 两个浮点数不等 */
#define UNEQUAL 0
实现部分。
/**
* @defgroup Line Line.c
* @{
* @code
Copyright (c) 2012, 广东小天才科技有限公司
All rights reserved.
文件名称: Line.cpp
文件标识: 关于直线方面的一些相关函数整理
文件摘要:
修改历史:
姓名 日期 说明
-------- ---------- --------------------
卢郴群 2012.12.31 程序开始编写
* @endcode
*/
#include "Line.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/**
* @brief
* @param [in]
* @return
* @par 修改历史:
* @code
姓名 日期 说明
-------- ---------- --------------------
卢郴群 2012.12.31 程序开始编写
* @endcode
*/
/**
* @brief 比较两个浮点数是否相等
* @param [in] a 浮点数
[in] b 浮点数
* @return 两个数是否相等
* - UNEQUAL 两个浮点数相等
* - EQUAL 两个浮点数不相等
* @par 修改历史:
* @code
姓名 日期 说明
-------- ---------- --------------------
卢郴群 2012.12.31 程序开始编写
* @endcode
*/
int CompareFloatNum(const float a, const float b)
{
if (fabs(a - b) < EPSINON)
{
return EQUAL;
}
return UNEQUAL;
}
/**
* @brief 已知两个点求两点的斜率
* @param [in] pointA 点A
[in] pointB 点B
* @return 两点的斜率
* @par 修改历史:
* @code
姓名 日期 说明
-------- ---------- --------------------
卢郴群 2012.12.31 程序开始编写
* @endcode
*/
float SlopeBetweenPoints(const Point *pointA, const Point *pointB)
{
assert(CompareFloatNum(pointA->x, pointB->x) != EQUAL);
return (pointA->y - pointB->y) / (pointA->x - pointB->x);
}
/**
* @brief 计算与给定直线相垂直的直线的斜率
* @param [in] slope 直线的斜率
* @return 与该直线相垂直的斜率
* @par 修改历史:
* @code
姓名 日期 说明
-------- ---------- --------------------
卢郴群 2012.12.31 程序开始编写
* @endcode
*/
float PerpSlope(const float slope)
{
assert(CompareFloatNum(slope, 0.0) != EQUAL);
return (-1.0 / slope);
}
/**
* @brief 判断两条线是否垂直
* @param [in] slope1 直线斜率
[in] slope2 直线斜率
* @return 两条线是否垂直的判断返回值
* - 1 两条直线相垂直
* - 0 两条直线不垂直
* @par 修改历史:
* @code
姓名 日期 说明
-------- ---------- --------------------
卢郴群 2012.12.31 程序开始编写
* @endcode
*/
int IsPrep(const float slope1, const float slope2)
{
if (CompareFloatNum(slope1 * slope2, -1.0))
{
return 1;
}
else
{
return 0;
}
}
/**
* @brief 求两条直线的交点
* @param [in] line1Slope 直线1的斜率
[in] line1Point 直线1上的一个点
[in] line2Slope 直线2的斜率
[in] line2Point 直线2上的一个点
[in] intersection 两条直线的交点
* @param [out] intersection 两条直线的交点
* @return 无
* @par 修改历史:
* @code
姓名 日期 说明
-------- ---------- --------------------
卢郴群 2012.12.31 程序开始编写
* @endcode
*/
void LineOfIntersection(const float line1Slope, const Point line1Point,
const float line2Slope, const Point line2Point,
Point *intersection)
{
assert(CompareFloatNum(line1Slope, line2Slope) != EQUAL);
intersection->x = ((line2Point.y - line2Slope * line2Point.x) - (line1Point.y - line1Slope * line1Point.x)) / (line1Slope - line2Slope);
intersection->y = line1Slope * (intersection->x - line1Point.x) + line1Point.y;
}
int main()
{
float slope1, slope2;
Point point1, point2, point;
int count = 100;
float temp1, temp2;
while (count--)
{
temp1 = rand();
slope1 = rand() % 20;
point1.x = rand();
point1.y = slope1 * point1.x + temp1;
temp2 = rand();
slope2 = rand() % 10;
point2.x = rand();
point2.y = slope2 * point2.x + temp2;
LineOfIntersection(slope1, point1, slope2, point2, &point);
printf("%f %f\n", point.x, (temp2 - temp1) / (slope1 - slope2));
if (CompareFloatNum( point.x, (temp2 - temp1) / (slope1 - slope2)) == UNEQUAL)
{
printf("Error\n");
}
}
system("pause");
return 0;
}
/** @}*/