Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "line.h"
#include <iostream>
Line::Line(){
}
Line::Line(const Line &graph){
}
Line &Line::operator=(const Line &graph){
}
Line::~Line(){
}
void Line::draw(QPainter &painter){
// Set brush color using hexadecimal value
setBrushForTheObject(painter);
painter.drawLine(getStartPoint(), getStopPoint());
}
void Line::move(QPoint vector){
setStartPoint(getStartPoint() + vector);
setStopPoint(getStopPoint() + vector);
}
float Line::getSteigung(){
float a = (float)(getStartPoint().y() - getStopPoint().y())/(getStartPoint().x() - getStopPoint().x());
return a;
}
float Line::getConstanteB()
{
float a = getSteigung();
float b = getStartPoint().y() - a * getStartPoint().x() ;
return b;
}
bool Line::checkTheSelectedShape(QPoint clicked){
// QPoint p = clicked;
// // swap start- and stop point if stop grather than start point
// if(getStartPoint().y() > getStopPoint().y()){
// QPoint y;
// y = getStopPoint();
// setStopPoint(getStartPoint());
// setStartPoint(y);
// }
// float a = getSteigung();
// float b = getConstanteB() ;
// float res = a * p.x() + b - p.y();
// return( getStartPoint().y() < p.y() && getStopPoint().y() > p.y() ) && ( res<RANGE && res> - RANGE) ;
// L(t) V
// p---------------------->
// |
// |
// R
// t0= (R - P).v/|v|^2
QPoint v = getStopPoint() - getStartPoint();
QPoint v_RP = clicked - getStartPoint();
//streckungsFaktor
float t = (float)(v_RP.x() * v.x() + v_RP.y() * v.y() ) / (sqrt(v.x()*v.x() + v.y() *v.y()) * sqrt(v.x()*v.x() + v.y() *v.y())) ;
// init lt and the cordonation of the POint must be positive abs() solve that problem
QPoint lt ; lt.setX((t * v.x())); lt.setY((t * v.y()));
lt = lt + getStartPoint();
std::cout<<" lt point "<<lt.x()<<" , "<<lt.y()<<std::endl;
// Vector Senkrecht zu der Line v = RL(t)
QPoint clicked_vector = lt - clicked ;
std::cout<<"clicked vector "<<clicked_vector.x()<<" , "<<clicked_vector.y()<<std::endl;
std::cout<<"Betrag "<<sqrt(clicked_vector.x()*clicked_vector.x() + clicked_vector.y() * clicked_vector.y())<<std::endl;
return ( t<=1 && t>=0 && RANGE >= sqrt( clicked_vector.x() * clicked_vector.x() + clicked_vector.y() * clicked_vector.y() ) );
}