-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangle.c
108 lines (68 loc) · 1.38 KB
/
rectangle.c
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
#include <stdio.h>
#include <stdlib.h>
#include "rectangle.h"
Rectangle * createRectangle(void) {
Rectangle *r;
Point *p;
r = malloc(1*sizeof(Rectangle));
p = malloc(1*sizeof(Point));
if(r == NULL || p == NULL) {
printf("malloc failed!");
exit(0);
}
p->x = 0;
p->y = 0;
r->height = 0;
r->width = 0;
r->origin = *p;
return r;
}
Rectangle * createRectangle2(Point p) {
Rectangle *r;
r = malloc(1*sizeof(Rectangle));
if(r == NULL) {
printf("malloc failed!");
exit(0);
}
r->height = 0;
r->width = 0;
r->origin = p;
return r;
}
Rectangle * createRectangle3(int w, int h) {
Rectangle *r;
Point *p;
r = malloc(1*sizeof(Rectangle));
p = malloc(1*sizeof(Point));
if(r == NULL || p == NULL) {
printf("malloc failed!");
exit(0);
}
p->x = 0;
p->y = 0;
r->height = h;
r->width = w;
r->origin = *p;
return r;
}
Rectangle * createRectangle4(Point p, int w, int h) {
Rectangle *r;
r = malloc(1*sizeof(Rectangle));
if(r == NULL) {
printf("malloc failed!");
exit(0);
}
r->height = h;
r->width = w;
r->origin = p;
return r;
}
/* change position to given x and y */
void move(Rectangle *r, int x, int y) {
/* poiner->foo same as (*p).foo */
r->origin.x = x;
r->origin.y = y;
}
int getArea(const Rectangle *r) {
return ( (*r).width * (*r).height) ;
}