original code given:
int[] x = {50,61,83,69,71,50,29,31,17,39};
int[] y = {18,37,43,60,82,73,82,60,43,37};
beginShape();
for(int i=0;i < x.length; i++)
vertex(x[i],y[i]);
endShape(CLOSE);
Add the appropriate methods so the star will appear at a random location. This method does NOT allow the user to select the location of a star.
Use a "for" loop in the setup method to call the star() method 100 times to produce 100 stars on the screen.
This is what I have so far...something obviously isn't right
void setup()
{
size(800,600);
background(#0F4D7C);
for (int i = 0; i < 100; i++)
{
randomStar();
}
}
void randomStar()
{
pushMatrix();
translate(0,0);
int[] x = {50,61,83,69,71,50,29,31,17,39};
int[] y = {18,37,43,60,82,73,82,60,43,37};
beginShape();
for(int i=0;i < x.length; i++)
vertex(x[i],y[i]);
endShape(CLOSE);
popMatrix();
}
You are missing the draw() method. I've also moved translate(x,y) to the for loop. Check it out:
void setup()
{
size(800, 600);
background(#0F4D7C);
noLoop();
}
void draw() {
for (int i = 0; i < 100; i++)
{
pushMatrix();
translate(random(width), random(height));
randomStar();
popMatrix();
}
}
void randomStar()
{
int[] x = {
50, 61, 83, 69, 71, 50, 29, 31, 17, 39
};
int[] y = {
18, 37, 43, 60, 82, 73, 82, 60, 43, 37
};
beginShape();
for (int i=0; i < x.length; i++)
vertex(x[i], y[i]);
endShape(CLOSE);
}
Related
I am trying to create the Sieve of Eratosthenes problem where I print out a grid from 2 to 100 and then cover all non-prime numbers with a rectangle. I can only get it to check one divisor between 2 and 10. I cant get it to loop through all divisors. my current version doesn't print any rectangles what so ever but it feels like it should be because reading it it looks like if variable a is less than 10 check if the number in that location is divisible by a. if it is print a rectangle there. once it checks all of those it add 1 to a. Where am I going wrong here?
int a=2;
void setup()
{
size(600, 600);
rectMode(CORNER);
textSize(17);
background(0);
for (int x = 0; x < 10; x++)
{
for (int y =0; y<11; y++)
{
if ((x)+((y-1)*10)+1>1)
{
fill(255);
text((x)+((y-1)*10)+1, x*50+30, y*50);
}
}
}
}
void draw()
{
for (int x = 0; x < 10; x++)
{
for (int y =0; y<10; y++)
{
while (a<10)
{
if ((x)+((y-1)*10)+1%a==0)
{
fill(50, 50, 200);
rect((x)*50+30, (y)*50+30, 30, 30);
}
a++;
}
}
}
}
You have several problems in Your solution.
((x)+((y-1)*10)+1>1) formula is overcomplex for this problem,
void draw() is a loop, so Your 'a' variable will never go back to value 2.
Use System.out.println(); wherever You are not sure what is going on.
So, here is the cleaner concept of Your problem:
in Your case there is no need to call draw(),
set 'a' value every time before while(), not just once before setup(),
draw Your boxes just inside the same place as text()
void setup() {
size(600, 600);
background(0);
fill(255);
for (int y=0; y<10; y++) {
for (int x=1; x<=10; x++) {
System.out.println(x + ", " + y + ": " + (x+y*10));
int a=2;
textSize(26);
while (a<=Math.sqrt(x+y*10)) {
if ((x+y*10)%a == 0) {
System.out.println(a+": "+x+", " +y+": "+(x+y*10));
textSize(12);
}
a++;
}
text(x+y*10, x*50, y*50+50);
}
}
}
void draw() {
}
I'm new to processing, and trying to do this task. Maybe you know how to do it, or know where I could read about it.
I need something like this:
Thanks in advance for your help.
The image is a 4x3 grid with zero spacing between the columns(vg) and rows(hg):
final int _numRows = 3;
final int _numCols = 4;
void rectGrid(int l, int t, int w, int h, int hg, int vg) {
int left;
int top;
for (int k = 0; k < _numRows; k++) {
for (int j = 0; j < _numCols; j++) {
left = l + j*(w+vg);
top = t + k*(h+hg);
stroke(0);
strokeWeight(2);
fill(255);
rect( left, top, w, h);
}
}
}
void setup() {
size(640, 340);
background(209);
rectGrid(20, 20, 150, 100, 0, 0);
}
I am a new coder on processing, that's why please be gentle.I made a simple code for you. Normally my code is more longer and complex.However, I wrote a simple code for you.
//main class
ArrayList<Clouds> clouds = new ArrayList();
void setup() {
size(1200, 800, P3D);
for (int i = 0; i < 3; i++)
{
Clouds C = new Clouds();
clouds.add(C);
}
}
void draw() {
background(0);
for (int i = 0; i < clouds.size(); i++)
{
clouds.get(i).drawClouds();
}
}
//Clouds class
class Clouds
{
float xC, yC, zC, speedC;
public Clouds()
{
xC = 20;
yC = 40;
zC = 0;
noStroke();
speedC = 1;
}
public void drawClouds()
{
translate(xC,yC);
pushMatrix();
makingClouds(100, 100, 100);
popMatrix();
if (xC > width - 780) {
xC = -660;
}
xC += speedC;
}
public void makingClouds(float xF, float yF, float zF ) {
translate(xF, yF, zF);
pushMatrix();
lights();
scale(1, 1, 1);
sphere(20);
popMatrix();
}
}
I hope, I'm not doing wrong with writing two class in here but I've worked on it two whole days and made me sick. So my question is: Like you see, there are three sphere and they have same speed but when I run the program, they go to end with different speeds. How they have same speed? If you help me, you will be my hero! Thank you.
translate() do not just set a translation, it defines a translation matrix and multiplies the new translation matrix to the current matrix.
You have to construct the clouds at different positions:
void setup() {
// [...]
for (int i = 0; i < 3; i++) {
Clouds C = new Clouds(20, i*40);
clouds.add(C);
}
}
class Clouds {
float xC, yC, zC, speedC;
public Clouds(float x, float y) {
xC = x;
yC = y;
zC = 0;
// [...]
}
And to move translate in the pushMatrix() / popMatrix() block:
class Clouds {
// [...]
public void drawClouds() {
pushMatrix();
translate(xC,yC);
// [...]
popMatrix();
// [...]
}
public void makingClouds(float xF, float yF, float zF ) {
pushMatrix();
translate(xF, yF, zF);
// [...]
popMatrix();
}
Example code:
//main class
ArrayList<Clouds> clouds = new ArrayList();
void setup() {
size(1200, 800, P3D);
for (int i = 0; i < 3; i++) {
Clouds C = new Clouds(20, i*40);
clouds.add(C);
}
}
void draw() {
background(0);
for (int i = 0; i < clouds.size(); i++) {
clouds.get(i).drawClouds();
}
}
//Clouds class
class Clouds {
float xC, yC, zC, speedC;
public Clouds(float x, float y) {
xC = x;
yC = y;
zC = 0;
speedC = 1;
}
public void drawClouds() {
noStroke();
pushMatrix();
translate(xC,yC);
makingClouds(100, 100, 100);
popMatrix();
if (xC > width - 780) {
xC = -660;
}
xC += speedC;
}
public void makingClouds(float xF, float yF, float zF ) {
pushMatrix();
translate(xF, yF, zF);
lights();
scale(1, 1, 1);
sphere(20);
popMatrix();
}
}
So the goal is add a black circle every time the hammer or the bucket drops without hitting the person and then add 4 circles every time the person goes inside the door, when the first line of circles is filled it will go onto the second line until the entire screen is filled and then it resets. Also with the person is hit by either the hammer or the bucket a whole row of of red circle will appear. I'm not sure how to go about doing it, thanks.
// all the variables I need for two tools
int hammerPos = 0;
int hammerVel = 1;
int hammerPending = 0;
int bucketPos = 0;
int bucketVel = 2;
int bucketPending = 0;
int mrPos;
int jump = 60;
boolean doorOpen = true;
long timeAt; // the time at which the door was last opened or closed
long waitTime; // the time to wait (in total) until the door opens next
void setup() {
size(512, 348);
mrPos = width/8;
frameRate(30);
waitTime = int(random(3000, 8000));
}
void draw() {
if (millis() > timeAt + waitTime) { // calculation will tell me if current time (millis) has gone past the last time the door opened plus waitTime
doorOpen = !doorOpen;
timeAt = millis(); // reset last opening time to now
waitTime = int(random(3000, 8000));// choose new waiting time
}
background(159, 172, 173);
rectMode(CORNERS);
ellipseMode(CORNERS);
noStroke();
house();
mrg();
hazard();
score();
}
void hazard() {
fill(0);
stroke(0);
strokeWeight(6);
line(128, hammerPos+10, 148, hammerPos-12);
strokeWeight(4);
line(125, hammerPos+13, 129, hammerPos+11);
strokeWeight(8);
line(115, hammerPos+5, 130, hammerPos+20);
// draw bucket
fill(0);
noStroke();
triangle(167, bucketPos-5, 187, bucketPos-12, 188, bucketPos+12);
triangle(167, bucketPos-5, 187, bucketPos+12, 169, bucketPos+14);
noFill();
stroke(0);
strokeWeight(4);
ellipse(183, bucketPos-11, 195, bucketPos+10);
if (bucketPos >height) {
bucketPos=0;
}
if (hammerPos>height) {
hammerPos=0;
}
if (hammerPos > 240 && hammerPos < 300 && mrPos == width/4) {
mrPos = width/8;
}
if (bucketPos > 240 && bucketPos < 300 && mrPos == 3*width/8) {
mrPos = width/8;
}
// animate all automatically moving objects.. first update pending then decide if there is a jump this frame
hammerPending = hammerPending + hammerVel;
bucketPending = bucketPending + bucketVel;
if (hammerPending > jump) {
hammerPos = hammerPos + hammerPending;
hammerPending = 0;
}
if (bucketPending > jump) {
bucketPos = bucketPos + bucketPending;
bucketPending = 0;
}
}
void score() {
if (hammerPos>height) {
ellipse(40, 40, 40, 40);
}
}
//draw house
void house() {
strokeWeight(20);
stroke(0);
line(402, 206, 490, 169);
line(490, 169, 512, 179);
strokeWeight(2);
noFill();
line(411, 213, 411, 305);
rect(419, 210, 468, 295);
if (doorOpen) {
line(479, 206, 499, 199);
line(499, 199, 499, 314);
line(499, 314, 478, 302);
fill(0);
noStroke();
triangle(476, 225, 493, 219, 493, 243);
triangle(476, 225, 493, 243, 476, 240);
ellipse(487, 255, 493, 263);
arc(499, 255, 507, 262, 3*HALF_PI, HALF_PI, CHORD);
} else {
fill(0);
noStroke();
rect(432, 225, 453, 243, 2);
ellipse(425, 255, 431, 263);
}
}
// draw MrGW
void mrg() {
noStroke();
fill(0);
ellipse(mrPos-13, 232, mrPos+17, 263);
fill(159, 172, 173);
ellipse(mrPos+3, 250, mrPos+17, 260);
fill(0);
triangle(mrPos+2, 258, mrPos-11, 278, mrPos+1, 285);
}
void keyPressed() {
if (keyCode == RIGHT && (mrPos < 7*(width/8)) && doorOpen) { // mr gw is moving right and the door is open
mrPos = mrPos + width/8;
} else if (keyCode == RIGHT && (mrPos < 6*(width/8))) { // mr gw is moving right not including last slot
mrPos = mrPos + width/8;
} else if (keyCode == LEFT && (mrPos > width/8)) { // mr gw is moving left.
mrPos = mrPos - width/8;
}
}
Basically you will need a variable for the number of points/circles: points. The circles itself should be drawn in the void loop, maybe with two for loops (see below).
Additionally, you will need to define, which of the circles should be red. In the following example I use a boolean Array, that defines for each circle if it should be red. So if the first circle has to be red, you have to set redPoint[0] to true...
int i=0;
for(int y=10; y<height-10; y+=20) {
for(int x=10; x<width-10; x+=20) {
if(redPoint[i] == true) {
fill(255,0,0);
} else {
fill(0);
}
if(i<points) {
ellipse(x,y,15,15);
}
i++;
}
}
I'm new to Processing. Why don't I see the first matrix drawn? I seem to only see the matrix after the delay, and not the one before. My ultimate goal is to watch how a matrix changes over time steps.
// Number of columns and rows in the grid
int[][] myArray = { {0, 1, 2, 3},
{3, 2, 1, 0},
{3, 5, 6, 1},
{3, 8, 3, 4} };
void setup() {
size(200,200);
}
void draw() {
background(204);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
rect(20+30*j,30+30*i,3,3);
}
}
delay(2500);
background(204);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
rect(40+30*j,50+30*i,7,7);
}
}
}
Your myArray variable is misleading, it doesn't seem to be used anywhere.
Basically you want to animate/interpolate between values.
Your code does this in the draw loop:
clear the background
draw 16 squares
wait 2500 ms
clear the background
draw 16 squares
which you'll tiny squares and after 2500 ms larger squares and that's it.
What want to do can be achieved in many ways, from the simpler to the more complex. Luckily Processing offers a lot of handy functions.
You want to store a property (like x position of a box) in a variable which you'll update over time and use the updated value to redraw on screen:
int x = 20;
int y = 30;
int w = 3;
int h = 3;
void setup() {
size(200,200);
}
void draw() {
//update
if(x <= 40) x++;
if(y <= 50) y++;
if(w <= 7) w++;
if(h <= 7) h++;
//draw
background(204);
for (int i = 0; i < 4 ; i++) {
for (int j = 0; j < 4; j++) {
rect(x+30*j,y+30*i,w,h);
}
}
}
You could also map() your values to a variable changing over time:
int x,y,s;
int xmin = 20,xmax = 40;
int ymin = 30,ymax = 50;
int smin = 3,smax = 7;
void setup() {
size(200,200);
}
void draw() {
//update
x = (int)map(mouseX,0,width,xmin,xmax);
y = (int)map(mouseX,0,width,ymin,ymax);
s = (int)map(mouseX,0,width,smin,smax);
//draw
background(204);
for (int i = 0; i < 4 ; i++) {
for (int j = 0; j < 4; j++) {
rect(x+30*j,y+30*i,s,s);
}
}
}
Or use linear interpolation (already implemented as lerp()):
int xmin = 20,xmax = 40;
int ymin = 30,ymax = 50;
int smin = 3,smax = 7;
void setup() {
size(200,200);
}
void draw() {
//update
float t = (float)mouseX/width;
//draw
background(204);
for (int i = 0; i < 4 ; i++) {
for (int j = 0; j < 4; j++) {
rect(lerp(xmin,xmax,t)+30*j,
lerp(ymin,ymax,t)+30*i,
lerp(smin,smax,t) ,
lerp(smin,smax,t) );
}
}
}
and you could alter your interpolation amount based on any variable you like:
int xmin = 20,xmax = 40;
int ymin = 30,ymax = 50;
int smin = 3,smax = 7;
void setup() {
size(200,200);
}
void draw() {
//update
float t = abs(sin(frameCount * .01));
//draw
background(204);
for (int i = 0; i < 4 ; i++) {
for (int j = 0; j < 4; j++) {
rect(lerp(xmin,xmax,t)+30*j,
lerp(ymin,ymax,t)+30*i,
lerp(smin,smax,t) ,
lerp(smin,smax,t) );
}
}
}
HTH