vector2D向量实例

/**
在x-y平面上给定四个顶点ABCD,确定它是否是一个矩形
*/
#include
class vector2D
{
public:
    vector2D(){}
    vector2D(float a,float b)
    { x=a; y=b; }///内联(1)
    vector2D difference(vector2D& a);
    float inner(vector2D& a);
    bool isPerpendicular(vector2D& a);
    bool nonzero()///内联(2)自动内联
    { return (x!=0.0||y!=0.0); }
    void display();
private:
    float x,y;
};
float vector2D::inner(vector2D&a)
{ return(x*a.x + y*a.y); }
void vector2D::display()
{ std::cout<<"("<0?x:-x); }
bool vector2D::isPerpendicular(vector2D& v){
    return (nonzero()&&v.nonzero()&&ABS(inner(v))<0.00000001);
}
vector2D getVec(int i){
    float x,y;
    std::cin>>x>>y;
    return vector2D(x,y);
}
int main()
{
    vector2D p[4];
    for(int i=0;i<4;i++)
        p[i]=getVec(i);
    vector2D u=p[0].difference(p[3]);
    vector2D v;
    for(int i=0;i<3;i++){
        v=p[i+1].difference(p[i]);
        if(!u.isPerpendicular(v))
        {
            std::cout<<"Not a rectangle.\n";
            return -1;
        }
        u=v;
    }
    std::cout<<"Yes\n";
    return 0;
}

你可能感兴趣的:(vector2D向量实例)