Grid of rectangles processing

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm trying to create filled rectangles in a 10x10 grid. I completely understand the logic behind it, and roughly how to put it into code, but there is one part I can't seem to get my head around; how I make the rectangles appear on the next line after the first 10 have been drawn.

As you can see, they go diagonally. I don't know how I can elegantly make them go on another row after reaching 10 rectangles. But that would A result in a lot of if statements and B It didn't even work they printed on the same line, still. Basically start drawing with one axis 10 rects using an inner loop, and repeating that with 40px shiftign for the other axis, repeating it 10 times JS Fiddle.

This code draws the rectangles column by column. Here's a fiddle. You can accomplish this using single if statement and a variable to keep track of number of boxes drawn in a row.

When 10 boxes are drawn reset the count and increment the y value. As others have stated you can have a for loop within a for loop, such that you increment each row first, then increment each column. Learn more. JS: How do I create a 10x10 grid of filled rectangles? Ask Question. Asked 4 years, 5 months ago.

Active 4 years, 5 months ago. Viewed 2k times. Any hints are appreciated, thanks! Basic math to the rescue. Once you hit width, you reset to left offset to 0 and increase top offset by Here is a working version of what you have: jsfiddle.

Active Oldest Votes. Pointy Pointy k 50 50 gold badges silver badges bronze badges. Mi-Creativity Mi-Creativity 8, 10 10 gold badges 31 31 silver badges 44 44 bronze badges. Oberdiah Oberdiah 33 6 6 bronze badges. Yes this would do it too! Nakib Nakib 3, 6 6 gold badges 19 19 silver badges 42 42 bronze badges. Derrick Derrick 2 2 silver badges 7 7 bronze badges.

Learn programming 34: create a grid of objects with nested while loops

Sign up or log in Sign up using Google.Processing Forum. Recent Topics.

Coordinate System and Shapes

Sub forum :. Move this topic Cancel. Programming Questions. Hi all; Programming newbie here. I'm developing a couple of things with processing but my programming skills are pretty rusty. Anyway, my programming question is as follows. I want to draw a grid formed by rectangles. The number of rectangles doesn't really matter.

I was able to draw the grid with two loops. Now, I need for each of these 'squares' to contain a number and an identifier so that further on I can detect collisions with each individual square.

Hopefully I was clear and someone can help me out : Thanks, Sam. Replies 5. Re: Programming: Drawing a grid with numbers 2 years ago. Post what you've done so far so that we can see how you're approaching this. Leave a comment on allonestring's reply. I don't really have much right now. I've got the grid going, but I had the help with a tutorial. So basically, inside each of the squares that this code generates, I'm hoping to get a number, preferably beginning at 1, and present it inside the square.

Then i'll be using the squares number as an identifier to detect collisions later on. Leave a comment on theformater's reply. Leave a comment on PhiLho's reply. Thanks phi. That's exactly what I needed. If you don't mind, could I get your help on a second question? How can I attribute to each of these single rectangles a unique identifier such that rectangle 1 is, for example, R1; rectangle 2 is R2 and so fort?

Should I be considering object oriented programming for this issue? I'm going to need that each individual rectangle detects if it's being 'colided' with by another object i'll be importing into a string. For such a simple set up, you can just detect the coordinates of the object against the computed coordinates of the rectangles. Topic Type : Discussions Questions.

Change topic type Cancel. Link this topic.

Making a grid of objects??

Provide the permalink of a topic that is related to this topic. Save Close. Reply to theformater's question. Top Reply. Tags Cancel.This is the second time we create a grid of objects. In the previous episode we used a while loop inside a while loop. Those loops increased two variables which were used to place rectangles in the screen.

In this episode we use again two nested while loops, but instead of counting pixels to fill the whole screen, we count how many rectangles we want to draw. Then we use a function from previous episodes that creates rotating rectangles.

We try different things: all rectangles rotating with exactly the same angle, using a different angle for rectangles depending on the column, depending on the row, and finally we give a different rotation angle for each rectangle in the screen depending on the column, the row and a variable that constantly increases to make them spin.

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects or hire me for help. A grid of rotating objects creates a wave of rectangles This is the second time we create a grid of objects. Tags: rotation, grid, while, loop, spinning Code editor.

You can make changes to the code below. Reference background draw float noStroke rect resetMatrix rotate setup size smooth translate void while.All rights reserved.

If you see any errors or have comments, please let us know. Before we begin programming with Processing, we must first channel our eighth grade selves, pull out a piece of graph paper, and draw a line. The shortest distance between two points is a good old fashioned line, and this is where we begin, with two points on that graph paper.

The above figure shows a line between point A 1,0 and point B 4,5. If you wanted to direct a friend of yours to draw that same line, you would give them a shout and say "draw a line from the point one-zero to the point four-five, please. The same command applies only this time you can skip the pleasantries and you will be required to employ a precise formatting. Here, the instruction will look like this:. Processing p5. Coordinate System and Shapes Daniel Shiffman Before we begin programming with Processing, we must first channel our eighth grade selves, pull out a piece of graph paper, and draw a line.

Here, the instruction will look like this: line 1,0,4,5 ; Even without having studied the syntax of writing code, the above statement should make a fair amount of sense. We are providing a command which we will refer to as a "function" for the machine to follow entitled "line. If you think of that line of code as a sentence, the function is a verb and the arguments are the objects of the sentence.

The code sentence also ends with a semicolon instead of a period. The key here is to realize that the computer screen is nothing more than a fancier piece of graph paper.

Each pixel of the screen is a coordinate - two numbers, an "x" horizontal and a "y" vertical - that determines the location of a point in space. And it is our job to specify what shapes and colors should appear at these pixel coordinates. Nevertheless, there is a catch here. The graph paper from eighth grade "Cartesian coordinate system" placed 0,0 in the center with the y-axis pointing up and the x-axis pointing to the right in the positive direction, negative down and to the left.

The coordinate system for pixels in a computer window, however, is reversed along the y-axis. Simple Shapes The vast majority of the programming examples you'll see with Processing are visual in nature.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm trying to make a grid of squares that change their fill from black to white and vice-versa when clicked. I'm able to turn the entire grid on or off currently, but I'm unable to figure out how to specify which particular square should be toggled when the mouse clicks within its borders.

I've created buttons using mouseX and mouseY coordinates before, but they were for specific objects that I could adjust manually.

I can't figure out how to do this using for loops and arrays. I've been told to create a boolean array and pass the value of that array to the grid array, but again, I don't know how to specify which part of the array it needs to go to. For example, how do I change the fill value of square [6][3] upon mousePressed?

First of all, you are currently recreating the entire board whenever the mouse is pressed. You must retain that info between mouse clicks, so make box a global array up there with the others.

Now, you've already written code that finds the coordinates for each box: You're passing it to rect!

grid of rectangles processing

All that is left to do is check whether the mouse is inside this rect, i. There are ways to calculate this without looping through the entire grid every time, but maybe the above helps you find the path yourself instead of just getting the code made for you. PS: The int a; int b; at the top does nothing and can be removed. You are using the local variables a and b in your function, which is correct.

Learn more. Creating a clickable grid in Processing 3 Ask Question. Asked 1 year, 5 months ago. Active 1 year, 5 months ago. Viewed times. Max Langhof HarrisonCarto HarrisonCarto 25 7 7 bronze badges. Do some arithmetic involving rows, cols, width, height, mouseX, mouseY to find the center of the square which is closest to mouseX, mouseY. Get out some scrap paper and figure out how to do it by hand for various choices.

Once you understand how it works, code it. JohnColeman I can figure out how to locate the mouse cursor without any issues. The part I'm stuck on is figuring out how to specify that a particular section of my array should be changed depending on the position of the cursor.

Or perhaps, specifying that a shape should be changed defined by a specific section of my grid array. Keep a boolean array say clicked with one entry per square in your grid.

When drawing the squares, check the array when deciding on the fill. Active Oldest Votes. Max Langhof Max Langhof I'm answering the question by showing the way to the solution. I think each step is now small enough that it makes for a good learning experience, and the answer is close enough to the original code that together with the explanations it becomes clear how one would have found these steps without help.

By the way, it was not me who downvoted your answer. Edit: Other comment was removed. Sign up or log in Sign up using Google.This sketch is created with an older version of Processing, and doesn't work on browsers anymore.

View Source Code. Drop files here or select. Also, many other sublime shortcuts are supported. Every time sketch is saved, a version history is created. You can view and restore below. Save as Fork Save Submit. Sketches Start coding in seconds, experiment, and create. Explore Sketches Create a Sketch. Classes Teach coding, collaborate, and showcase class work. Explore Classes Create a Class.

Archived Sketch This sketch is created with an older version of Processing, and doesn't work on browsers anymore. Grid: rectangles. How to interact with it e. Tags e. Who can see your sketch? Who can see the code? Who can comment? Delete Sketch? Learn more. Showcase Sketch. Infinite Loop Protection. Save or fork the sketch to upload files. Get More Space. Text Size. Layout This will be the default layout for your sketches.

grid of rectangles processing

Keyboard Shortcuts Also, many other sublime shortcuts are supported. Post Cancel. Join to Comment. Grid: rectangles by Jerome Herr. Create a Fork. Create GIF. Sign in to Download.This sketch is created with an older version of Processing, and doesn't work on browsers anymore. View Source Code. Drop files here or select. Also, many other sublime shortcuts are supported. Every time sketch is saved, a version history is created.

You can view and restore below. Save as Fork Save Submit. Sketches Start coding in seconds, experiment, and create. Explore Sketches Create a Sketch. Classes Teach coding, collaborate, and showcase class work. Explore Classes Create a Class. Archived Sketch This sketch is created with an older version of Processing, and doesn't work on browsers anymore. Rectangle grid.

grid of rectangles processing

Simple grid with rectangles. How to interact with it e. Tags e. Who can see your sketch? Who can see the code? Who can comment? Delete Sketch? Learn more. Showcase Sketch. Infinite Loop Protection. Save or fork the sketch to upload files. Get More Space. Text Size.

fun grid of colourful squares

Layout This will be the default layout for your sketches. Keyboard Shortcuts Also, many other sublime shortcuts are supported. Post Cancel. Join to Comment. Rectangle grid by Rafael Rinaldi.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *