跳至主要內容

数据类型相关

umbrella大约 5 分钟

写在前面

  • 本篇主要介绍在Rocos/share/geomotry.h中定义的类(class)。

  • 内容 部分提供了部分类型的私有成员仅用于方便理解用。

  • 构造 部分的 lua 部分传入参数的类型可以参考 cpp 部分。

常用数据类型

CVector

  • 二维向量,常用于表示速度等。

  • 内容

    • double _x 向量的 x 值。
    • double _y 向量的 y 值。
  • 构造

lua
-- 传入坐标 x,y 值
local vel = CVector(x, y)
  • 常用方法
    • double mod() 返回向量的模。
    • double mod2() 返回向量的模的平方。
    • double dir() 返回向量的角度。
    • double theta(const CVector &v) 返回自身到给定向量的夹角。
    • double x() 获取 x 值,还有返回 y 值的y()
    • CVector operator+(const CVector &v) 返回当前向量加上传入向量。
    • CVector operator-(const CVector &v) 返回当前向量减去传入向量。
    • CVector operator*(double a) 返回当前向量的x, y乘上a
    • double operator*(CVector b) 返回当前向量的向量点乘。
    • CVector operator/(double a) 返回当前向量的x, y除以a
    • CVector operator-() 返回当前向量的负。
lua
-- 使用方法
local vel = CVector(x1, y1)

-- mod()
local velMod = vel:mod()

-- dir()
local veldir = vel:dir()

-- x,y
local vel1X = vel:x()
local vel1Y = vel:y()

CGeoPoint

  • 二维的坐标点。

  • 内容

    • double _x 坐标的 x 值。
    • double _y 坐标的 y 值。
  • 构造

lua
-- 传入坐标 x,y 值
local point = CGeoPoint(x, y)
  • 常用方法
    • double x() 获取 x 值,还有返回 y 值的y()
    • double dist(const CGeoPoint &p) 返回传入坐标与当前坐标的距离。
    • double dist2(const CGeoPoint &p) 返回传入坐标与当前坐标的距离的平方。
    • CGeoPoint midPoint(const CGeoPoint &p) 返回传入坐标与当前坐标的中点坐标。
    • bool operator==(const CGeoPoint &rhs) 返回当前坐标与传入坐标是否相同。
    • CGeoPoint operator+(const CVector &v) 返回传入坐标与当前坐标的和。
    • CVector operator-(const CVector &v) 返回传入坐标与当前坐标的差。
    • CGeoPoint operator*(const double &a) 返回当前坐标的x, y乘上a
lua
-- 使用方法
local point1 = CGeoPoint(x1, y1)
local point2 = CGeoPoint(x2, y2)

-- 我们仔细观察可以发现两个 CGeoPoint 相减之后的类型是 CVector 
-- 所以我们相减过后可以调用 CVector 的方法 
-- 例如 mod()
local mod = (point1 - point2):mod()

-- dist()
local dist = point1:dist(point2)
-- 其实 mod == dist

CGeoLine

  • 直线(区别于线段)。

  • 内容

    • CGeoPoint _p1 直线的第一个坐标点。
    • CGeoPoint _p2 直线的第二个坐标点。
  • 构造

lua
-- 传入直线的起点和直线的方向
local line1 = CGeoLine(point1, point2)
local line2 = CGeoLine(point, dir)
  • 常用方法

    • 返回投影点坐标。
    cpp
    CGeoPoint projection(const CGeoPoint &p)
    

    关于投影点坐标:

    关于投影点坐标
    关于投影点坐标
    • CGeoPoint point1() 返回点 1。
    • CGeoPoint point2() 返回点 2。
    • bool operator==(const CGeoLine &rhs) 返回当前实现是否与传入直线相同。
lua
-- 使用方法
local line = CGeoLine(point1, point2)

-- projection()
local projectionPos = line:projection(point3)

-- point1,point2
local point1 = line:point1()
local point2 = line:point2()

CGeoLineLineIntersection

  • 直线与直线的交点。

  • 内容

    • bool _intersectant 两条直线是否相交。
    • CGeoPoint _point 两条直线的交点。
  • 构造

cpp
// 传入两条直线(CeoLine)
CGeoLineLineIntersection(const CGeoLine &line_1, const CGeoLine &line_2)
  • 常用方法
    • bool Intersectant() 两条直线是否相交。
    • CGeoPoint IntersectPoint() 两条直线的交点。

CGeoSegment

  • 线段(区别于直线),继承自直线。

  • 内容

    • CGeoPoint _start 线段的起点。
    • CGeoPoint _end 线段的终点。
  • 构造

lua
-- 传入线段的起点和终点
local segment = CGeoSegment(point1, point2)
  • 常用方法
    • bool IsPointOnLineOnSegment(const CGeoPoint &p) 直线上的点是否在线段上。
    • CGeoPoint segmentsIntersectPoint(const CGeoSegment &p) 和传入线段的交点,如果没有交点会返回CGeoPoint(9999, 9999)
    • double dist2Point(const CGeoPoint &p) 返回传入点到当前线段的距离。
    • double dist2Segment(const CGeoSegment &s) 返回传入线段到当前线段的距离,如果相交返回0.
    • CGeoPoint start() 返回起点,还有返回终点的end()和返回中点的center()

常用数据类型的方法

提示

根据这里所介绍的,该板块的这些常用方法的适用范围不仅仅只有演示中的数据类型,可以自行实践。

  • mod() 取两个坐标之间的模长。

    用法示例:

    lua
    local distance = (point1 - point2):mod()
    
  • dir() 取两个坐标的方向。

    用法示例:

    lua
    local direction = (point1 - point2):dir()
    

总结

  • 在 lua 中我们想要使用几何相关的数据类型及其方法,可以去Rocos/Core/src/LuaModule/geometry.pkg中查阅,并根据具体的定义去正确使用。
  • c++ 则需要到相应的Rocos/share/geometry.hRocos/share/geometry.cpp文件中查阅。