The sketch below produces two vector fields. Part of the third last method called calculateField() is commented out. Uncommenting that section calculates an average for theta and magnitude for the two fields and should then plot a single vector field which would be the sum of the two fields.
Firstly the fields are not summing correctly and secondly moving the charges, only one seems to effect the overall field. (The fields can be changed by holding down the left mouse button and dragging the charges around.)
int numCharges = 2;//the number of charges
Electrons[] charge = new Electrons[numCharges];//Declare the 1D array called 'charge', the elements of which are objects of the class called 'Electrons'
int numVectorsX = 21;//20*30 that's number of vectors across by spacing between each is width and add 1 to the number of vectors in the x-direction
int numVectorsY = 11;//10*30 that's number of vectors down by spacing between each is height and add 1 to the number of vectors in the y-direction
int distance = 30;//spacing between vectors, same for x and y directions
Field[][] vectors = new Field[numVectorsX][numVectorsY];//Declare the 2D array called 'vectors', the elements of which are objects of the class called 'Field'
float targetX = 300, targetY = 150, pol, fieldStrength, minDiameter, maxDiameter;//the field initially points to a singularity at the coordinate (300,150). targetX and targetY is where the field points to. 'pol' is the polarity of the charge
float theta = 0;//angle between charge and each vector
float magnitude = 0;//length or magnitude of a vector
float fieldTransparency = 0;
int r, g, b;//red green blue variables for charge colour
float Fieldstrength;
int countCharges = -1;
void setup() {
size(600, 300);
smooth();
noStroke();
for (int k = 0; k < numCharges; k++) {//these loops populate the 2D array, each element of this array is an object and each object is a vector
//x, y, r, g, b, transparency, count, initial diameter, minChargediameter, maxChargediameter... the fields
vectors[i][j] = new Field(fieldTransparency, i*distance, j*distance, magnitude, theta, maxdist, countCharges);
}
}
}
void draw() {
background(#DAEBF2);
for (int k = 0; k < numCharges; k++) {//these loops populate the 2D array, each element of this array is an object and each object is a vector
charge[k].move();
charge[k].display();
}
for (int i = 0; i < numVectorsX; i++) {
for (int j = 0; j < numVectorsY; j++) {
for (int k = 0; k < numCharges; k++) {
vectors[i][j].calculateField();
vectors[i][j].update(charge[k].x, charge[k].y, charge[k].diameter, charge[k].minChargediameter, charge[k].maxChargediameter);//puts the fields x, y etc. from the object 'charge' into the object 'vectors', there they are set equal to targetX, targetY, etc. which is where the field points to
vectors[i][j].polarity(charge[k].r);//puts the field r from the object 'charge' into the object 'vectors', there it is set equal to 'pol'.
//vectors[i][j].plotField();
}
}
}
}
void mouseReleased() {
if (mouseButton == CENTER) {
for (int k = 0; k < numCharges; k++) {//these loops populate the 2D array, each element of this array is an object and each object is a vector
charge[k].released();
}
}
}
class Electrons {
float x, y, r, g, b, transparency, diameter, minChargediameter, maxChargediameter;
float theta = atan((targetY-yField)/(targetX-xField));//(targetX,targetY) coordinates of the charges. (xField,yField) coordinates of point at which vector is defined.
float d = (sqrt(pow(targetY-yField, 2) + pow(targetX-xField, 2)));//should be 1/r^2 but not practical here
if (targetX < xField) {//without this the vectors on the left of screen will point away from charge and vectors on right of screen toward it. All should point away or toward it
theta += PI;
}
if (pol == 0) {//if 'pol' is 0 then r = 0 in charge.released and so charge is blue and positive
theta += PI; //points the field away from the charge position for a positive charge
maxFieldstrength = map(fieldStrength, minDiameter, maxDiameter, newVectorlengthmin, newVectorlengthmax);//maps the fieldStrength, which is the diameter of the charge, from the max and mix charge diameter to the max and min charger length
if (d > maxFieldstrength) {
magnitude = map(d, 0, maxdist, maxFieldstrength, minFieldstrength);//the second last value is the maximum length of a vector. 2nd last value should a min and last a max value, inverting them puts longest vectors near charge
fieldTransparency = fieldTransparency/numCharges;//calculates average
theta = theta/numCharges;//calculates average
magnitude = magnitude/numCharges;//calculates average
*/
stroke(1, fieldTransparency);
fill(0, 0, 0);
pushMatrix();
translate(xField, yField);
rotate(theta);
line(0, 0, magnitude, 0);
beginShape();//this shape is the arrow heads of the vectors
vertex(magnitude, -3);
vertex(magnitude, 3);
vertex(magnitude + 3, 0);
endShape(CLOSE);
popMatrix();
noFill();
noStroke();
//} THIS BRACKET IS COMMENTED OUT
}
void update(float x, float y, float diameter, float minChargediameter, float maxChargediameter) {//this method transfers fields x, y, diameter, ... etc from the class Electrons to the class Field
I am trying to pass a variable called elapsedTime (line 20.) from an Ardunio sketch to a Processing sketch. I know in the Processing I can't read into a float like this. A float is 4 bytes and a read reads one byte. (line 3 of the Processing sketch)
The Ardunio code is as follows,
int ledTX = 12; //Receives InfraRed signal
int photodiodePin = 2; // Photodiode connected to digital pin 2
int lastState;
unsigned long startTime;
unsigned long elapsedTime;
void setup() {
Serial.begin(9600);
pinMode(ledTX, OUTPUT);
pinMode(photodiodePin, INPUT); // sets the digital pin as input to read photodiode
}
void loop() {
byte currentState = digitalRead(photodiodePin);
digitalWrite(ledTX, HIGH); // turn the TX Infrared LED on
if(currentState == LOW && lastState == HIGH){
startTime = millis();
}
if(currentState == HIGH && lastState == LOW){
elapsedTime = millis() - startTime;
Serial.println(elapsedTime);
Serial.write(elapsedTime);
}
lastState = currentState;
}
The Processing code is as follows,
import processing.serial.*;
Serial port;
float elapsedTime;
void setup() {
size(200, 200);
background(204);
noStroke();
port = new Serial(this, 9600);
}
void draw() {
if (0 < port.available()) {
elapsedTime = port.read();
}
println(elapsedTime);
}
I was getting help with this
here in the Arduino forum, but am stuck now.
I want to create a vector field where each vector is an object and each object is an element in an array. But at the moment i am only trying to draw the tail of the vectors, I will hopefully be able to add a head to these vectors later. At the moment the sketch is only producing the tail of one vector and not the others.
int numVectors = 16;
Field[] vectors = new Field[numVectors];
int r, g, b;
void setup() {
size(600, 300);
int arrayx = width/5;
int arrayy = height/4;
smooth();
noStroke();
//The following 'for' loops populate the objects
for (int i = 0; i < 16; i++) {//i is the object element number
for (int j = 0; j < 5; j++) {//j is for the x coordinate
for (int k = 0; k < 5; k++) {//k is for the y coordinate
// x1, y1, x2, y2
vectors[i] = new Field(10*j, 10*k, (10*j)+10, (10*k)+10);
I see there are three methods: move(), finished() and display() but what about .remove which seems to me a method but isn't in the class? Is this a built in function of some sort? Where can I read about this in the reference and also the 'add' in balls
.add(new Ball(mouseX, mouseY, ballWidth));
I am trying to create a sketch that does the following:
LEFT mouse button allows circle to be moved
RIGHT mouse button allows circle size to change when moving toward and away from centre
CENTRE mouse button changes colour of circle from red to blue. A second click should bring it back to its previous fill colour. This last thing is what I am stuck on.
I would very much appreciate if anyone has any ideas on how this can be done.
float x = 300, y = 150, diameter=100;
void setup() {
size(600, 300);
stroke(1);
}
void draw() {
frameRate(24);
background(#36B5F2);
fill(255);
if (((dist(mouseX, mouseY, x, y))<(diameter/2))&&(mousePressed == true)) {
if (mouseButton == LEFT) {
x = mouseX;
y = mouseY;
}
else if (mouseButton == RIGHT) {
if (((dist(mouseX, mouseY, x, y))-(dist(pmouseX, pmouseY, x, y)))>(0)) {
diameter++;
}
else if (((dist(mouseX, mouseY, x, y))-(dist(pmouseX, pmouseY, x, y)))<(0)) {
I wrote a sketch using arrays and then tried to rewrite the sketch using OOP, thinking this would be less memory and processing intensive for my computer. But I am surprised that the OOP sketch is crashing.
In the following simple sound wave simulation the compression lags behind the wave slightly, by about 1/8 of a wavelength. Is there any smple way to correct this? I've tried but only been able to move the wave left a bit but that's not correct.
I am trying to simulate how particles behave in a sound wave.
I have a random array of dots which oscillate horizontally. But I'd like the oscillation of each to only begin when their x-coordinate is the same as that of a red dot which travels horizontally across the screen.
The code for the dots all oscillating together is,
int numDots = 10;
float x, angle, frequency = PI;
float xpos;
float vel = 0.1;
int[]ArrayX = new int[numDots];
int[]ArrayY = new int[numDots];
void setup() {
size (600, 300);
for (int i = 0; i < numDots; i++) {
ArrayX[i] = (int) random(0, width);
ArrayY[i] = (int) random(0, height);
}
}
void draw() {
background (127);
stroke(0);
fill(0);
for (int i = 0; i < numDots; i++) {
x = sin(radians(angle))*50;
ellipse(ArrayX[i]+x, ArrayY[i], 3, 3);
angle -= frequency/32;
noStroke();
fill(250, 0, 0);
ellipse(xpos, 100, 5, 5);
stroke(0);
fill(0);
xpos += vel;
}
}
and to try to get them to only oscillate when the red dot passes using the 'if' statement on line 20:
int numDots = 10;
float x, angle, frequency = PI;
float xpos;
float vel = 0.1;
int[]ArrayX = new int[numDots];
int[]ArrayY = new int[numDots];
void setup() {
size (600, 300);
for (int i = 0; i < numDots; i++) {
ArrayX[i] = (int) random(0, width);
ArrayY[i] = (int) random(0, height);
}
}
void draw() {
background (127);
stroke(0);
fill(0);
for (int i = 0; i < numDots; i++) {
angle -= frequency/32;
if (xpos==ArrayX[i]) {
x = sin(radians(angle))*50;
ellipse(ArrayX[i]+x, ArrayY[i], 3, 3);
}
noStroke();
fill(250, 0, 0);
ellipse(xpos, 100, 5, 5);
stroke(0);
fill(0);
xpos += vel;
}
}
I don't know why the array of dots can't be seen in the second code.
I'm trying to create a random array of ellipses. But I want to save athe coordinates of each in an array. I want this array only to be generated once within draw() and then to be able to use that array again with in draw.
Below is my attempt,
int cols = 10; int rows = 10; int[]ArrayX = new int[rows]; int[]ArrayY = new int[rows];
if (frameCount==1) { for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { int x = (int) random(-10, 10); int y = (int) random(-10, 10); ArrayX[i] = x + 10*i; ArrayY[j] = y + 10*j; } } } ellipse(ArrayX[i], ArrayY[j], 3, 3); }
The following code produces an array of numbers. But there seem to be numbers over numbers and I am not sure how to fix this.
I think the problem may be to do with the fact that the two functions are called from inside draw() and so are endlessly running. I tried placing the call to them outside of this but inside draw() seems to be the only place i can get them to work.
Any help would be greatly appreciated.
Thanks,
Shane
int cols = 5; int rows = 10; int xn0 = 1; int yn0 = 1; float h = 0.1; float[][] myArray = new float[cols][rows]; // declares array
I'm trying to control a variable inside draw() with data from the serial port. The serial port is connected to an Arduino with a simple potentiometer circuit.
I can read in from the serial port to a variable called inByte. But then how do I work with that inside draw()? The code is below.
I have seen how processing can be used to to export a frame or image to pdf but is it possible to make a pdf of data which would normally be output to the console?