计算几何学习,第一天

向量运算,点线关系

点积:a·b=|a||b|cosθ
几何意义:向量b在向量a上的投影与a的长度的乘积
坐标运算:a·b=x1*x2+y1*y2
应用
        1.判断两向量垂直a·b=0

        2.判断两向量平行a·b=|a||b|

        3.求两向量的夹角cosθ=a·b/|a||b|
证明:
a·b=abcosθ=ab*(a^2+b^2-c^2)/2ab
={(x1^2+y1^2)+(x2^2+y2^2)-[(x1-x2)^2+(y1-y2)^2]}/2
=x1*x2+y1*y2
 

operator知识复习

自定义的operator+运行流程
friend Point operator+(Point a, const Point& b) {
    return a += b;
}
定义一个全局的友元函数,然后执行a+=b操作

operator+=的运行流程
Point& operator+=(const Point& p)& {
    x += p.x;
    y += p.y;
    return *this;
}
调用自身成员的x,y,加上另一个成员的变量
*this返回的是当前对象的引用而不是拷贝
比如说a+=b会变成
a.operator+=(b),左值接受*this的返回

例如
Point p3=p1+p2
转换成p3=Point.operator+(p1,p2)
a=p1;
转换成p3=Point.operator+(a,p2)
转换成p3=a.operator+=(p2)
转换成a=*this
转换成p3=a

下面是基本的点的结构与功能

template<class T>
struct Point {
    T x;
    T y;
    //初始值设为0
    Point(const T& x_ = 0, const T& y_ = 0) :x(x_), y(y_) {}
    //类型转换模版
    //Point<int> p1(3, 4);       定义一个整数点
    //Point<double> p2 = p1;     转换为双精度点
    template<class U>
    operator Point<U>() {
        return Point<U>(U(x), U(y));
    }
    Point& operator+=(const Point& p)& {
        x += p.x;
        y += p.y;
        return *this;
    }
    Point& operator-=(const Point& p)& {
        x -= p.x;
        y -= p.y;
        return *this;
    }
    Point& operator*=(const T& v)& {
        x *= v;
        y *= v;
        return *this;
    }
    Point& operator/=(const T& v)& {
        x /= v;
        y /= v;
        return *this;
    }
    friend Point operator+(Point a, const Point& b) {
        return a += b;
    }
    friend Point operator-(Point a, const Point& b) {
        return a -= b;
    }
    friend Point operator*(Point a, const T& b) {
        return a *= b;
    }
    friend Point operator/(Point a, const T& b) {
        return a /= b;
    }
    friend Point operator*(const T& a, Point b) {
        return b *= a;
    }
    friend bool operator==(const Point& a, const Point& b) {
        return a.x == b.x && a.y == b.y;
    }
    friend std::istream& operator>>(std::istream& is, Point& p) {
        return is >> p.x >> p.y;
    }
    friend std::ostream& operator<<(std::ostream& os, const Point& p) {
        return os << "(" << p.x << ", " << p.y << ")";
    }
};

template<class T>//a和b的点积
T dot(const Point<T>& a, const Point<T>& b) {
    return a.x * b.x + a.y * b.y;
}

template<class T>
T square(const Point<T>& p) {
    return dot(p, p);
}

template<class T>//求模长
double length(const Point<T>& p) {
    return std::sqrt(square(p));
}
template<class T>//求cosθ
double angle(Point<T> a, Point<T> b) {
    return std::acos(dot(a, b) / length(a) / length(b));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值