-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.c
More file actions
116 lines (87 loc) · 2.39 KB
/
test.c
File metadata and controls
116 lines (87 loc) · 2.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
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
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <math.h> // sqrt
/**********************\
* *
* STRUCT & TYPEDEF *
* *
\**********************/
typedef struct Point {
double x;
double y;
} Point;
typedef struct Line {
Point a;
Point b;
} Line;
typedef struct Vector {
double x;
double y;
} Vector;
/**********************\
* *
* POINT CLASS *
* *
\**********************/
Point point_create(double x, double y) {
return (Point){x,y};
}
void point_display(Point p) {
printf("Point (%f,%f)\n",p.x,p.y);
}
/**********************\
* *
* LINE CLASS *
* *
\**********************/
Line line_create(Point a, Point b) {
return (Line){a,b};
}
void line_display(Line l) {
printf("Line (%f,%f) -> (%f,%f)\n",l.a.x,l.a.y,l.b.x,l.b.y);
}
Vector line_get_vector(Line l) {
return (Vector){l.b.x-l.a.x,l.b.y-l.a.y};
}
/**********************\
* *
* VECTOR CLASS *
* *
\**********************/
Vector vector_create(double x, double y) {
return (Vector){x,y};
}
void vector_display(Vector v) {
printf("Vector (%f,%f)\n",v.x,v.y);
}
double vector_dot(Vector v1, Vector v2) {
return v1.x*v2.x + v1.y*v2.y;
}
double vector_length(Vector v) {
return sqrt(vector_dot(v,v));
}
Vector vector_normalize(Vector v) {
double length = vector_length(v);
return (Vector){v.x/length,v.y/length};
}
Vector vector_add(Vector v1, Vector v2) {return (Vector){v1.x+v2.x,v1.y+v2.y};}
Vector vector_sub(Vector v1, Vector v2) {return (Vector){v1.x-v2.x,v1.y-v2.y};}
Vector vector_add_scalar(Vector v, double s) {return (Vector){v.x+s,v.y+s};}
Vector vector_sub_scalar(Vector v, double s) {return (Vector){v.x-s,v.y-s};}
Vector vector_mul_scalar(Vector v, double s) {return (Vector){v.x*s,v.y*s};}
Vector vector_div_scalar(Vector v, double s) {return (Vector){v.x/s,v.y/s};}
/**********************\
* *
* MAIN *
* *
\**********************/
///[ENTRY_POINT]
int main() {
Point mypoint = point_create(5.0,2.0);
point_display(mypoint);
Point mypoint2 = point_create(1.0,4.0);
Line myline = line_create(mypoint,mypoint2);
line_display(myline);
Vector myvector = line_get_vector(myline);
vector_display(myvector);
return 0;
}