Skip to content
Snippets Groups Projects
line.cpp 1.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • Pelotrio's avatar
    Pelotrio committed
    #include <math.h>
    #include <iostream>
    
    
    #include "line.h"
    #include "scene.h"
    
    
    Line::Line(Point startIn, Scene *s) : Shape(startIn, s->getDefaultColour()) {
    }
    
    void Line::setEnd(Point endIn) {
        end = Point(endIn.x - start.x, endIn.y - start.y);
    }
    
    void Line::display(QPainter *qp) {
        qp->setPen(QPen(colour, 2, Qt::SolidLine));
        qp->drawLine(QPoint(start.x, start.y), QPoint(start.x + end.x, start.y + end.y));
    }
    
    void Line::displayHighlight(QPainter *qp, QColor colourIn) {
        qp->setPen(QPen(colourIn, 2, Qt::DashLine));
        qp->drawLine(QPoint(start.x, start.y), QPoint(start.x + end.x, start.y + end.y));
    }
    
    void Line::update(Point in, Scene *parent) {
        setEnd(in);
    }
    
    float Line::innerDistance() {
        return distanceBetweenPoints(start, end);
    }
    
    bool Line::pointInShape(Point in) {
        AlgLine selfLine(start, start + end);
        Point meet;
        try {
            meet = linesIntersect(selfLine, orthogonalLine(selfLine, in));
        } catch (AlgError e) {
            return false;
        }
    
        //std::cout << meet.x << " " << meet.y << "\n";
        //std::cout << in.x << " " << in.y << "\n";
        //std::cout << distanceBetweenPoints(in, meet) << "\n\n\n";
    
        Point delta = in - start;
        if (delta.x < min(0, end.x) || delta.y < min(0, end.y) || delta.x > max(0, end.x) || delta.y > max(0, end.y)) {
            return false;
        }
        if (distanceBetweenPoints(in, meet) <= 3) {
            return true;
        }
        return false;
    }