13. Drawing Examples
Dated: 12-05-2025
Drawing Table
A table consists of a table top and 4 legs, all rectangular.
Design
Constraints
- \(640 \times 480\) screen
- width = 20, length = 14, height = 10
- Screen top means \(y = 0\) and screen bottom means \(y = 480\)
- Table center is at the screen center
typedef struct {
int x;
int y;
} Point;
Point points[4] = {
{310, 233},
{330, 233},
{330, 247},
{310, 247}
};
legLength = 10;
Point center = {
(points[0].x + points[1].x) / 2,
(points[1].y + points[2].y) / 2
};
void translate(int x, int y) { // move by (x, y)
for (int i = 0; i < 4; i++) {
points[i].x += x;
points[i].y += y;
}
}
void rotate(float theta) { // rotate by theta angle
for (int i = 0; i < 4; i++) {
points[i].x = center.x + (points[1].x - center.x) * cos(theta) - (points[i].y - center.y) * sin(theta);
points[i].y = center.y + (points[1].x - center.x) * sin(theta) + (points[i].y - center.y) * cos(theta);
}
}
void scale(int x, int y) { // scale by (x, y)
for (int i = 0; i < 4; i++) {
points[i].x = center.x + (points[i].x - center.x) * x;
points[i].y = center.y + (points[i].y - center.y) * y;
}
legLength *= y;
}
void drawTable () {
// Table Top
line(points[0].x, points[0].y, points[1].x, points[1].y);
line(points[1].x, points[1].y, points[2].x, points[2].y);
line(points[2].x, points[2].y, points[3].x, points[3].y);
line(points[3].x, points[3].y, points[0].x, points[0].y);
// Legs
line(points[0].x, points[0].y, points[0].x, points[0].y + legLength);
line(points[1].x, points[1].y, points[1].x, points[1].y + legLength);
line(points[2].x, points[2].y, points[2].x, points[2].y + legLength);
line(points[3].x, points[3].y, points[3].x, points[3].y + legLength);
}