I just downloaded the new Processing and auto format has changed. It now takes a bunch of one line conditionals like this:
if (in[i] == 1) out += (i%4) + (i>>2);
else if (in[i] == 2) out += abs((i%4)-1) + (i>>2);
else if (in[i] == 3) out += abs((i%4)-2) + (i>>2);
else if (in[i] == 4) out += abs((i%4)-3) + (i>>2);
else if (in[i] == 5) out += (i%4) + abs((i>>2)-1);
else if (in[i] == 6) out += abs((i%4)-1) + abs((i>>2)-1);
else if (in[i] == 7) out += abs((i%4)-2) + abs((i>>2)-1);
else if (in[i] == 8) out += abs((i%4)-3) + abs((i>>2)-1);
else if (in[i] == 9) out += (i%4) + abs((i>>2)-2);
else if (in[i] == 10) out += abs((i%4)-1) + abs((i>>2)-2);
else if (in[i] == 11) out += abs((i%4)-2) + abs((i>>2)-2);
else if (in[i] == 12) out += abs((i%4)-3) + abs((i>>2)-2);
else if (in[i] == 13) out += (i%4) + abs((i>>2)-3);
else if (in[i] == 14) out += abs((i%4)-1) + abs((i>>2)-3);
else if (in[i] == 15) out += abs((i%4)-2) + abs((i>>2)-3);
else if (in[i] == 0) continue;
And turns it into this (all one line):
if (in[i] == 1) out += (i%4) + (i>>2); else if (in[i] == 2) out += abs((i%4)-1) + (i>>2); else if (in[i] == 3) out += abs((i%4)-2) + (i>>2); else if (in[i] == 4) out += abs((i%4)-3) + (i>>2); else if (in[i] == 5) out += (i%4) + abs((i>>2)-1); else if (in[i] == 6) out += abs((i%4)-1) + abs((i>>2)-1); else if (in[i] == 7) out += abs((i%4)-2) + abs((i>>2)-1); else if (in[i] == 8) out += abs((i%4)-3) + abs((i>>2)-1); else if (in[i] == 9) out += (i%4) + abs((i>>2)-2); else if (in[i] == 10) out += abs((i%4)-1) + abs((i>>2)-2); else if (in[i] == 11) out += abs((i%4)-2) + abs((i>>2)-2); else if (in[i] == 12) out += abs((i%4)-3) + abs((i>>2)-2); else if (in[i] == 13) out += (i%4) + abs((i>>2)-3); else if (in[i] == 14) out += abs((i%4)-1) + abs((i>>2)-3); else if (in[i] == 15) out += abs((i%4)-2) + abs((i>>2)-3); else if (in[i] == 0) continue;
This is really quite annoying, can it be set to the old way again somehow?
I was asked to give an Intermediate level workshop on Processing at my lab. Two other people will be giving a Beginner and Advanced level workshops and all of the workshops will be 50 minutes.
I was wondering if there is a good estimate of what an Intermediate Processing user would or would not know. I looked at the Learning section (
http://processing.org/learning/ ) to get an idea, and it seems like it is mostly how to manipulate the Coordinate System, Trigonometry, and custom shapes.
If you feel like you are in this range I'd appreciate knowing what kind of Processing sketches you are working on. The workshop is at The University of Illinois in Chicago on the 23rd if anyone is interested in attending.
Edit:
From 4:00 - 8:00 at the Electronic Visualization Labratory (EVL)
I'm trying polymorphism in Processing for the first time and have a class Sphere that extends class Shape. I have an ArrayList<Shape> called objects and when I try to access a variable that is in Shape like:
println(objects.get(0).n);
There is no problem. The variable, n, above does not appear in Sphere, only in Shape (Sphere inherits it). On the other hand accessing something that is in Sphere but not in Shape does not seem to work:
println(objects.get(0).radius);
I get an error message that tells me "radius cannot be resolved or is not a field". How can I go about accessing this? Code in case that is helpful:
ArrayList<Shape> objects;
PVector light, prp, vrp;
float uMin, uMax, vMin, vMax;
void setup() {
size(800, 800, P2D);
loadPixels();
/* Just one light for now:
Without the code to handle otherwise, it is assumed
I was just starting to write a class for vectors and noticed that my normalize method appears as blue. It doesn't seem to cause an error. When is it problematic to use text that Processing highlights?
I'm practicing parametric intersections of a line and a circle. There can be 0, 1, or 2 intersections among the two. If there are less than 2 intersections I end up with NaN as one of my intersection variables (which is fine, the equation is trying to calculate an intersection that doesn't exist). What is the standard way to test for NaN? Right now I just am drawing the intersection point if its intersection variable is greater than 0:
I'm making a Binary Search tree for the first time. So far it seems to work, but I would like to be able to test if I am inserting a duplicate element. Right now it simply doesn't insert anything if the tree already contained the element and prints a message saying "Duplicate". Can I make this return a boolean so I could test if it didn't insert anything?
I realize that I could use contains() first, but I'd prefer insert() to return a boolean. Also, as this is the first time I'm writing a Binary Search tree any pointers on improvements are welcome.
BTree tree = new BTree();
void setup() {
size(400, 400);
tree.insert(4);
tree.insert(8);
tree.insert(8);
tree.insert(2);
tree.insert(3);
println(tree.contains(2));
}
void draw() {
}
class BTree {
Node root;
BTree() {
root = null;
}
boolean contains(int in) {
return contains(in, root);
}
boolean contains(int in, Node curr) {
if (curr == null) return false;
if (in < curr.val) return contains(in, curr.left);
else if (in > curr.val) return contains(in, curr.right);
else return true;
}
boolean isEmpty() {
return root == null;
}
void insert(int in) {
root = insert(in, root);
}
Node insert(int in, Node curr) {
if (curr == null) return new Node(in, null, null);
if (in < curr.val) curr.left = insert(in, curr.left);
else if (in > curr.val) curr.right = insert(in, curr.right);
I have some programs I wrote for OpenGL where positive y-axis is up. When I convert them to Processing everything is upside down (Processing positive y-axis is down). What is the easiest way to visually reverse this without changing all the math of the original program?
I started school this semester and am working in Java and C++, both of which are completely new to me. I had used Processing for a while (~4 years) and am accustomed to learning programming visually. In C++ I am using OpenGL to work visually by modifying class projects but do not know where to start for Java.
Does anyone know of a good place to learn how to make a window that can be drawn to like Processing? I have seen examples that create an applet for web but I would prefer to find something that could run in a terminal (if that is possible).
I have to manually write the values of a double array for a sketch I am making for later reference. I know that a normal array can be written like this (preferred):
So far I'm just working in 2D getting faces to subdivide. The linked wiki page has steps for how to do the algorithm and the first one says to find the centroid of a face. I've never tried to do that, and I have a hunch my method (highlighted in red below) is not finding the centroid. Can anyone verify this and help if I am doing it wrong?
Note:
I know I am not moving the original vertices of the faces in the code below (one of the steps of the wiki article says to do that). I'm not worried about that step yet.
It took me a long time to figure out the subdividing recursion. The data structure I'm using is something I made up that works but may not be the best. I'd gladly take suggestions on improvement.
I have continued working on the polygon sketch I shared here a few times but have come up against an error I cannot figure out. To trigger the error:
- Click on a polygon. It will make "selectors" appear, clicking on one of them adds a new polygon in that direction
- If that new polygon (or any new polygon after) is selected and then the mouse clicks on an area that is not a "selector" the error happens
- The error does not happen after new polygons are added if the mouse clicked on a "selector"
Unfortunately Processing doesn't tell me which line caused the error, and I can't seem to figure it out. If anyone could help me figure it out I'd really appreciate it. Some side notes about the sketch:
- Right now if a new polygon is added it is correctly placed. However, if vertices are added / removed it no longer is tangent to its parent polygon. This is not really an error / problem so much as I have not implemented a correction to it yet as I am distracted by the error
- I have not optimized all of the code by the suggestions from earlier threads on this sketch (I promise I will, the suggestions were wonderful, thank you again to everyone that helped)
float unit = 50;
float selUnit = unit/4;
int selected = -1;
ArrayList<Polygon> polys = new ArrayList<Polygon>();
I wrote a sketch where I can select among regular polygons on the screen accurately with the mouse. Right now the selection is done with the mouse, but in a future sketch it will probably be performed many times by an AI planning tessellations. The code highlighted in red below works but I want to make sure it is optimized before I write something that will use that test many times.
On a side note, the method below (again, in red) is the only way I could think of to accurately select the area of a regular polygon. If someone knows of a better way I'd love to know:
float unit = 50;
boolean somethingSelected = false;
int selected = 0;
ArrayList<Polygon> polys = new ArrayList<Polygon>();
I have a sketch that can increase / decrease the number of vertices for a polygon. Right now I have a variable, "unit", which is set to 50. All of the polygons' vertices are drawn from the center of the polygon with a distance of 50 from the center. This is not what I actually want, I want all of the edges of the polygons to have the length of 50 (polygons with more vertices will be bigger than polygons with less vertices). How can I do this?
float unit = 50;
int selected = 0;
ArrayList<Polygon> polys = new ArrayList<Polygon>();
I am trying to set up a structure that will let me add or remove polygons from a future geometry sketch. I also want to be able to change the number of vertices for a polygon and the vertices' positions. I have never used an ArrayList of ArrayLists and can't see what is wrong with the last line in setup() (highlighted in red). This structure seems a bit complicated, if there is a suggestion of a better way to do it I am willing to change the structure.
ArrayList<ArrayList> polygons = new ArrayList<ArrayList>();
I have only used HashMap once so I am not too familiar with it. What I am trying to do is build a list of URLs I have visited with a spider. If the spider has not visited a site then it should be added to the HashMap to get visited later (Note, in the code below the spider does not visit sites stored in the HashMap yet).
Right now if I am adding a new URL (stored as a String) to the HashMap I just store the String and assign the key to be 1. Does it matter what the key is for my purpose? I really am only interested in having a list of sites not visit to avoid repetition of visitation. I imagine the list will become large when it visits multiple sites so I figured a HashMap was the way to go. Here is the code so far:
I am using: loadStrings("http://www.processing.org/") right now for testing. When I try to load a URL that is invalid I get an error. How can I set up a conditional to test if it will fail and only perform loadStrings() if it doesn't fail without the error?
Is it possible to use Processing to visit a website and get the source code? I wanted to try to see if I could build something that visits a website and finds keywords I choose.
I came across a nice fractal zooming web app the other day and made one like it. I have not included color in my own version and was wondering what would be a good way to make a draggable color selection like the one from this link:
http://www.carefullyconstructed.co.uk/projects/fractal/
To see what I am talking about in the link click on the downward pointing arrow, a menu will appear. I realize this is not Processing but I want to make a sketch of something like the color selection in Processing before I try to make it work with my web app.
I'm not sure how to go about starting something like this. Basically what I am looking to do is have an arbitrary number of nodes that are assigned a color and a x position between 0 and 1 to make a color palette. An image that is made of pixels with values between 0 and 1 will receive the appropriate color from this palette. Any help is appreciated.
I have a program that draws a fractal using pixels[]. I want to be able to zoom in by clicking and dragging a rect over a section to make that the new boundaries (then redraw the fractal with the new boundaries). It takes a almost a second to calculate the fractal and draw it so I don't want to draw it every frame. Is there a way to set pixels once so I could call it like a background and be able to drag a rect on top of it?
This would be the equivalent of what I want but with background:
int mx, my;
void setup() {
size(600, 600);
noFill();
stroke(255);
}
void draw() {
background(128); // I want this to be a call to show what is in pixels[]
if (mousePressed) rect(mx, my, mouseX-mx, mouseY-my);
I'm starting a new project for a Chess AI and am just doing the drag and drop selection for human players at the moment. The code I have below works fine, but when I start doing move generation it is going to be really important that the code is as fast as possible.
Right now more than one piece can be on a square (haven't coded a condition to prevent it yet) but later that will never happen. What I would like to be able to do is pick a square on the board (right now with the mouse) and know which long I should be doing my bitwise operations on (there is a long for white pawns, another for white rooks, another for white knights, ect). Right now I just test all of the longs in my array with a bitmask to see if they intersect the square I have selected (with the mouse). It would be ideal not to test all of the longs in an array and just point to the right element somehow, help would be greatly appreciated.
EDIT: I put in comments to help and highlighted the code I was talking about in red.
boolean somethingSelected, wTurn;
long mSelect;
long[] pieces = new long[12];
PImage[] pieceImgs = new PImage[12];
void setup() {
size(801, 801);
stroke(128);
pieceImgs[0] = loadImage("WhitePawn.png");
pieceImgs[1] = loadImage("WhiteRook.png");
pieceImgs[2] = loadImage("WhiteKnight.png");
pieceImgs[3] = loadImage("WhiteBishop.png");
pieceImgs[4] = loadImage("WhiteQueen.png");
pieceImgs[5] = loadImage("WhiteKing.png");
pieceImgs[6] = loadImage("BlackPawn.png");
pieceImgs[7] = loadImage("BlackRook.png");
pieceImgs[8] = loadImage("BlackKnight.png");
pieceImgs[9] = loadImage("BlackBishop.png");
pieceImgs[10] = loadImage("BlackQueen.png");
pieceImgs[11] = loadImage("BlackKing.png");
initialize();
}
void draw() {
display();
}
void mousePressed() {
somethingSelected = false;
mSelect = 1L<<(7-mouseX/100+(7-mouseY/100)*8);
if (wTurn) { // If it is White's turn...
for (int i = 0; i < 6; i++) { // Test elements corresponding to White pieces
if ((mSelect&pieces[i]) != 0) {
somethingSelected = true;
break;
}
}
}
else { // If it is Black's turn...
for (int i = 6; i < 12; i++) { // Test elements corresponding to Black pieces
I'm trying to design a new chess set in Processing and I have run into a problem with the first piece (the knight). At the top of the knight there is an arc that goes around the knight's head and there are internal lines from the beginShape that I don't want inside that arc (I need to hide them somehow. The lines I'm talking about look like the left, top, and right of a box). I can't find an elegant way to hide these lines. Any help / suggestions would be appreciated.
On a different note, can anyone think of an easier way to design something that looks like this in Processing? This method is not exactly the easiest way to create 2D work.
Sorry, this is probably a very dumb question, but I've never used long. The reference said that long uses 64 bits instead of int's 32. I tried this out:
long test = 0xfffffffff; // 9 fs
void setup() {
size(100, 100);
println(test);
}
and I got the error "The literal 0xfffffffff of type int is out of range". What am I doing wrong? Also, is there a way to print a long as 0s and 1s?