#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Simple Rectangle Program - Don't Understand A Body of Code

edited July 2016 Posts: 3

The code below is a program that creates rectangles that obey gravity from touches on the screen. I don't have any troubleshooting questions for this, but I want to know what a certain block of code means. The entire program is below followed by the section I don't understand.

The Entire Code:

``````-- Main Class

function setup()
print("Welcome to BOX STACKER!!!")

boxes = {}

local ground = Box:createBox(WIDTH/2, 10, WIDTH, 20, 0)
ground.type = STATIC
table.insert(boxes, ground)

end

function touched(touch)
if touch.state == BEGAN then
table.insert(boxes, Box:createBox(touch.x, touch.y, math.random(20, 80), math.random(20, 80), 0, vec2(touch.x, touch.y)))
end
end

function collide(contact)
if contact.state == BEGAN then
sound(DATA, "ZgNAHAAfQEBATExAAAAAAFBxST5jD7M+TABAf0BAQEBAQEBA")
end
end

function draw()
background(40, 40, 50)

for k, box in pairs(boxes) do
Box:drawBox(box)
end

end

-- Box Class

Box = class()

function Box:createBox(x, y, w, h, r)
local box = physics.body(POLYGON, vec2(-w/2, h/2),
vec2(-w/2, -h/2), vec2(w/2, -h/2), vec2(w/2, h/2))

box.x = x
box.y = y
box.angle = r
box.interpolate = true
box.restitution = 0

return box
end

function Box:drawBox(box)
pushStyle()
pushMatrix()

stroke(148, 224, 135, 255)
translate(box.x, box.y)
rotate(box.angle)

if box.shapeType == POLYGON then
strokeWidth(3.0)
local points = box.points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
end

popMatrix()
popStyle()
end
``````

The Section I Don't Understand:

``````function Box:drawBox(box)
pushStyle()
pushMatrix()

stroke(148, 224, 135, 255)
translate(box.x, box.y)
rotate(box.angle)

if box.shapeType == POLYGON then
strokeWidth(3.0)
local points = box.points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
end

popMatrix()
popStyle()
end
``````

Can anyone explain what this section of code means line by line? I want to be able to understand it so I can utilize it in other programs. I'd appreciate the effort. Thank you.

Tagged:

• Posts: 110

Hi, `pushStyle()` and `popStyle()` pretty much just make sure the colour of stroke or fill do not carry over to other parts of the program. Push and pop matrix do the same for transformations. `stroke()` sets the colour of the stroke, `translate()` translates (so the new origin is the centre of the box) and `rotate` rotates so that the x-axis points in the direction of the box's angle. Then it checks if the shape is a polygon and if it is it sets the stroke width to something else. Next it makes a local table which has the points of the box on it. The for loop goes through each of the points of the box and draws a line from that point to the next point (`(j % #points)+1` will be j+1 for j<#points and 1 if j=#points)
Note that this draws any polygon, not just a rectangle.

• Posts: 3

Thank you very much!

• Mod
Posts: 8,397

@TheBroskateers Just a suggestion. Anytime you don't understand what code does, try looking in the build in reference and read what the command does. That way you get a full explanation of each command. Also, you might want to just skim thru the reference just to see what's there. You'll find some interesting thing there for later use.

• Posts: 3

Thank you too. I do read the reference a lot, but sometimes I just don't understand the technical jargon, and I need someone to explain it in more laymen terms.

• Mod
Posts: 5,396

@TheBroskateers - I had a similar problem, so I wrote some ebooks to hopefully help people like yourself, here

https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa