// **********************************************************
// File: Segment.h
// Description: Definition of class Segment
//
// Author: Carlos Moreno
// **********************************************************
#ifndef __SEGMENT_H__
#define __SEGMENT_H__
#include "Point.h"
class Segment
{
public:
// Constructors
explicit Segment (double x1 = 0, double y1 = 0,
double x2 = 0, double y2 = 0)
: p1 (x1, y1), p2 (x2, y2) {}
explicit Segment (const Point & _p1, const Point & _p2)
: p1 (_p1), p2 (_p2) {}
// Geometric and miscelaneous operations
void draw () const;
// Platform dependent - Implemented for Win32
bool intersects (const Segment &) const;
bool contains (const Point &) const;
// returns true if point is on the segment
// Comparison operators
bool operator== (const Segment & s) const;
bool operator!= (const Segment & s) const;
// get utility functions
const Point & get_p1 () const
{
return p1;
}
const Point & get_p2 () const
{
return p2;
}
private:
Point p1, p2;
};
#endif
// **********************************************************
// File: Segment.cpp
// Description: Implementation of class Segment
//
// Author: Carlos Moreno
// **********************************************************
#include "Segment.h"
#include "Triangle.h" // orientation function
#include "graphic_interface.h"
bool Segment::intersects (const Segment & s) const
{
return turn (p1, p2, s.p1) == -turn (p1, p2, s.p2) &&
turn (s.p1, s.p2, p1) == -turn (s.p1, s.p2, p2);
}
bool Segment::contains (const Point & p) const
{
return turn (p1, p2, p) == collinear &&
((p-p1) * (p-p2) < 0);
// If the scalar product is negative, it means
// p1-p and p2-p are in opposite senses; therefore,
// p is between p1 and p2
}
void Segment::draw () const
{
draw_line (p1.get_x(), p1.get_y(), p2.get_x(), p2.get_y());
}
bool Segment::operator== (const Segment & s) const
{
return p1 == s.p1 && p2 == s.p2;
}
bool Segment::operator!= (const Segment & s) const
{
return !(*this == s);
}
//End of File