Drawing a house with functions
in
Programming Questions
•
7 months ago
Ok, so I have the code to draw a house given to me by my professor , but when I try using functions to make it work, nothing happens.Once I start throwing voids in there It kills the program. Obviously I'm doing something wrong here and I'd appreciate any insight from anyone.
Here are the assignment instructions:
The functions that you should write are:
- void drawFrameArea()
Which draws the bottom half of the house. - void drawRoofArea()
Which draws the top half of the house.
These two functions will call the following functions:
- void drawFrame()
Which draws the brick frame of the house. - void drawWindow(float xCenter, float yCenter)
Which draws one window whose center is located at (xCenter, yCenter) including the lit window and the framing bars. - void drawDoor()
Which draw the door and the doorknob.
- void drawChimney()
Which draws the brick chimney. - void drawRoof()
Which draws to triangular roof.
The code that calls the top-level, area, functions will be called from the function:
- void setup()
And heres my code.
// Define the size of the sketch.
void setup(){
final int WIDTH = 500, HEIGHT = WIDTH; // Square canvas.
// Set sketch canvas size and smooth lines.
size(WIDTH, HEIGHT);
smooth();
}
// Define colors of the components.
final color BLACK = color( 0, 0, 0);
final color BRICK = color(160, 0, 0);
final color ROOF = color( 0, 128, 0);
final color LIGHT = color(255, 255, 0);
final color WOOD = color(128, 96, 64);
final color KNOB = color(224, 192, 64);
// Define the proportions (fractions) used to build the house.
final float HORIZ_MARGIN = 0.10 * WIDTH;
final float VERT_MARGIN = 0.10 * HEIGHT;
final float FRAME_LEFT = HORIZ_MARGIN;
final float FRAME_RIGHT = WIDTH - HORIZ_MARGIN;
final float FRAME_WIDTH = FRAME_RIGHT - FRAME_LEFT;
final float FRAME_HEIGHT = 0.50 * HEIGHT;
final float FRAME_BOTTOM = HEIGHT - VERT_MARGIN;
final float FRAME_TOP = FRAME_BOTTOM - FRAME_HEIGHT;
final float ROOF_LEFT = FRAME_LEFT;
final float ROOF_RIGHT = FRAME_RIGHT;
final float ROOF_CENTER = (ROOF_LEFT + ROOF_RIGHT) / 2;
final float ROOF_WIDTH = FRAME_WIDTH;
final float ROOF_HEIGHT = 0.30 * HEIGHT;
final float ROOF_BOTTOM = FRAME_TOP;
final float ROOF_TOP = ROOF_BOTTOM - ROOF_HEIGHT;
final float CHIMNEY_LEFT = 0.90 * ROOF_WIDTH;
final float CHIMNEY_WIDTH = 0.10 * ROOF_WIDTH;
final float CHIMNEY_HEIGHT = 0.75 * ROOF_HEIGHT;
final float CHIMNEY_TOP = ROOF_BOTTOM - CHIMNEY_HEIGHT;
final float WINDOW_WIDTH = 0.15 * FRAME_WIDTH;
final float WINDOW_HEIGHT = 0.30 * FRAME_HEIGHT;
final float LEFT_WINDOW_CENTER = FRAME_LEFT + 0.25 * FRAME_WIDTH;
final float RIGHT_WINDOW_CENTER = FRAME_LEFT + 0.75 * FRAME_WIDTH;
final float WINDOW_VERT_CENTER = FRAME_TOP + 0.30 * FRAME_HEIGHT;
final float DOOR_WIDTH = 0.15 * FRAME_WIDTH;
final float DOOR_HEIGHT = 0.40 * FRAME_HEIGHT;
final float DOOR_HORIZ_CENTER = FRAME_LEFT + FRAME_WIDTH / 2;
final float DOOR_VERT_CENTER = FRAME_BOTTOM - DOOR_HEIGHT / 2;
final float DOOR_KNOB_HORIZ_CENTER = DOOR_HORIZ_CENTER + 0.40 * DOOR_WIDTH;
final float DOOR_KNOB_VERT_CENTER = DOOR_VERT_CENTER;
final float DOOR_KNOB_DIAM = 0.10 * DOOR_WIDTH;
float left, right, top, bottom;
// Draw the frame of the house in BRICK.
void drawFrame(){
fill(BRICK);
rect(FRAME_LEFT, FRAME_TOP,
FRAME_WIDTH, FRAME_HEIGHT);
}
// Draw the chimney of the house in BRICK (before the roof).
void drawChimney(){
rect(CHIMNEY_LEFT, CHIMNEY_TOP,
CHIMNEY_WIDTH, CHIMNEY_HEIGHT);
}
// Draw the roof of the house in ROOF.
void drawRoof(){
fill(ROOF);
triangle(ROOF_LEFT, ROOF_BOTTOM,
ROOF_RIGHT, ROOF_BOTTOM,
ROOF_CENTER, ROOF_TOP);
}
// Draw the windows.
void drawWindow(){
fill(LIGHT);
rectMode(CENTER);
rect(LEFT_WINDOW_CENTER, WINDOW_VERT_CENTER,
WINDOW_WIDTH, WINDOW_HEIGHT);
rect(RIGHT_WINDOW_CENTER, WINDOW_VERT_CENTER,
WINDOW_WIDTH, WINDOW_HEIGHT);
// Draw the window panes.
left = LEFT_WINDOW_CENTER - WINDOW_WIDTH / 2;
right = LEFT_WINDOW_CENTER + WINDOW_WIDTH / 2;
line(left, WINDOW_VERT_CENTER, right, WINDOW_VERT_CENTER);
top = WINDOW_VERT_CENTER - WINDOW_HEIGHT / 2;
bottom = WINDOW_VERT_CENTER + WINDOW_HEIGHT / 2;
line(LEFT_WINDOW_CENTER, top, LEFT_WINDOW_CENTER, bottom);
left = RIGHT_WINDOW_CENTER - WINDOW_WIDTH / 2;
right = RIGHT_WINDOW_CENTER + WINDOW_WIDTH / 2;
line(left, WINDOW_VERT_CENTER, right, WINDOW_VERT_CENTER);
top = WINDOW_VERT_CENTER - WINDOW_HEIGHT / 2;
bottom = WINDOW_VERT_CENTER + WINDOW_HEIGHT / 2;
line(RIGHT_WINDOW_CENTER, top, RIGHT_WINDOW_CENTER, bottom);
}
// Draw the door.
void drawDoor(){
fill(WOOD);
rect(DOOR_HORIZ_CENTER, DOOR_VERT_CENTER, DOOR_WIDTH, DOOR_HEIGHT);
// Draw the door knob.
noStroke();
fill(KNOB);
ellipse(DOOR_KNOB_HORIZ_CENTER, DOOR_KNOB_VERT_CENTER,
DOOR_KNOB_DIAM, DOOR_KNOB_DIAM);
}
void drawFrameArea(){
drawFrame();
drawWindow();
drawDoor();
}
void drawRoofArea(){
drawRoof();
drawChimney();
}
I'm new at this, and I should probably research this a bit more but I would appreciate help because I've been trying this (presumably simple) task for hours. Thanks.
1