-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstick.cpp
More file actions
68 lines (49 loc) · 1.39 KB
/
stick.cpp
File metadata and controls
68 lines (49 loc) · 1.39 KB
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
//
// Created by Avery Drennan on 4/27/23.
//
#include "stick.h"
#include <cmath>
#pragma once
stick::stick(point *p0, point *p1, double length)
{
is_highlighted = false;
is_active = true;
this->p0 = p0;
this->p1 = p1;
this->length = length;
}
point* stick::get_p0() {
return p0;
}
point* stick::get_p1() {
return p1;
}
void stick::update()
{
std::pair<double,double>p0_pos = std::make_pair(p0->get_x(),p0->get_y());
std::pair<double,double>p1_pos = std::make_pair(p1->get_x(),p1->get_y());
std::pair<double,double> diff = std::make_pair(p0_pos.first - p1_pos.first,p0_pos.second - p1_pos.second);
double distance = sqrt(diff.first * diff.first + diff.second * diff.second);
double diff_factor = (length-distance)/distance;
std::pair<double,double> offset = std::make_pair(diff.first * diff_factor * 0.5f,
diff.second * diff_factor * 0.5f);
p0->set_x(p0_pos.first + offset.first);
p0->set_y(p0_pos.second + offset.second);
p1->set_x(p1_pos.first - offset.first);
p1->set_y(p1_pos.second - offset.second);
}
void stick::highlight() {
this->is_highlighted = true;
}
bool stick::highlighted() {
return this->is_highlighted;
}
void stick::remove_highlight() {
is_highlighted = false;
}
void stick::Break() {
is_active = false;
}
bool stick::active() {
return is_active;
}