More than one attractor point.
in
Programming Questions
•
10 months ago
Hello,
I am working on a script that is based on one point attraction.
First of all, the script has two classes "Electrons" and "Field".
For now, I managed to create two points by names charge[k] and charge2[k].
Then I bumped into the line, that tells
x, y, r, g, b, transparency, count, initial diameter, minChargediameter, maxChargediameter... the fields
---> THIS CONTROLS THE POINT LOCATION AND OTHER THINGS THAT EVENTUALLY CONTROLS THE ATTRACTION ---->VARIABLE MAGNITUDE.
The problem I have - I want to have more than one attractor point.
Is it possible to modify the script to do that?
The script only creates one array of arrows that is modified by only one point.
If you create let say 3, 5, 7 points. The script makes 3,5,7 separate grids of arrows. They all are not connected with each other.
I would like to create not a separate grids of arrows, but only one grid of arrows and their direction could be changed by multiple attracting points.
Best regards,
Petras Vestaras
I am working on a script that is based on one point attraction.
First of all, the script has two classes "Electrons" and "Field".
For now, I managed to create two points by names charge[k] and charge2[k].
Then I bumped into the line, that tells
x, y, r, g, b, transparency, count, initial diameter, minChargediameter, maxChargediameter... the fields
---> THIS CONTROLS THE POINT LOCATION AND OTHER THINGS THAT EVENTUALLY CONTROLS THE ATTRACTION ---->VARIABLE MAGNITUDE.
- vectors[i][j].update(charge[k].x+charge2[k].x, charge[k].y+charge2[k].x, charge[k].diameter, charge[k].minChargediameter, charge[k].maxChargediameter);
I thought I could multiple or do other mathematical operation with points' location in a grid of arrows. But
the result becomes just as using one point, but in a distorted way, let say... Ouch...
Anyway, if you do not have time to run the script, please at least say how two attractor points affect the grid?
the result becomes just as using one point, but in a distorted way, let say... Ouch...
Anyway, if you do not have time to run the script, please at least say how two attractor points affect the grid?
The problem I have - I want to have more than one attractor point.
Is it possible to modify the script to do that?
The script only creates one array of arrows that is modified by only one point.
If you create let say 3, 5, 7 points. The script makes 3,5,7 separate grids of arrows. They all are not connected with each other.
I would like to create not a separate grids of arrows, but only one grid of arrows and their direction could be changed by multiple attracting points.
Best regards,
Petras Vestaras
- int numCharges = 1;
Electrons[] charge = new Electrons[numCharges];
Electrons[] charge2 = new Electrons[numCharges];
int numVectorsX = 21;
int numVectorsY = 21;
int distance = 30;
Field[][] vectors = new Field[numVectorsX][numVectorsY];
float targetX = 300, targetY = 150, pol, fieldStrength, minDiameter, maxDiameter;
float theta = 0;
int magnitude;
float Fieldstrength;
void setup() {
size(600, 600);
smooth();
noStroke();
for (int k = 0; k < numCharges; k++) {
//////////////////////////////////////////////////////////////////////////////////
//x, y, r, g, b, transparency, count, initial diameter, minChargediameter, maxChargediameter... the fields
charge[k] = new Electrons(100+(200*k), 100+(50*k), 0, 0, 0, 100, 1, 100, 20, 400);
charge2[k] = new Electrons(400+(200*k), 100+(50*k), 0, 0, 0, 100, 1, 100, 20, 400);
//////////////////////////////////////////////////////////////////////////////////
}
float maxdist = 400;
for (int i = 0; i<numVectorsX; i++) {
for (int j = 0; j < numVectorsY; j++) {
vectors[i][j] = new Field(i*distance, j*distance, magnitude, theta, maxdist);
}
}
}
void draw() {
fill(255, 30);
rect(0, 0, 1000, 1000);
for (int k = 0; k<numCharges; k++) {
charge[k].move();
charge[k].display();
charge2[k].move();
charge2[k].display();
}
for (int k = 0; k<numCharges;k++) {
for (int i=0; i<numVectorsX;i++) {
for (int j=0; j<numVectorsY; j++) {
vectors[i][j].plot();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vectors[i][j].update(charge[k].x+charge2[k].x, charge[k].y+charge2[k].x, charge[k].diameter, charge[k].minChargediameter, charge[k].maxChargediameter);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vectors[i][j].polarity(charge[k].r);
}
}
}
/*
for (int k = 0; k<numCharges;k++) {
for (int i=0; i<numVectorsX;i++) {
for (int j=0; j<numVectorsY; j++) {
vectors[i][j].plot();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vectors[i][j].update(charge2[k].x, charge2[k].y, charge2[k].diameter, charge2[k].minChargediameter, charge2[k].maxChargediameter);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vectors[i][j].polarity(charge2[k].r);
}
}
}
*/
}
void mouseReleased() {
if (mouseButton == CENTER) {
for (int k = 0; k<numCharges;k++) {
charge[k].released();
}
}
if (mouseButton == CENTER) {
for (int k = 0; k<numCharges;k++) {
charge2[k].released();
}
}
}
class Electrons {
float x, y, r, g, b, transparency, diameter, minChargediameter, maxChargediameter;
int count;
Electrons(float xpos, float ypos, float rpos, float gpos, float bpos, float trans, int countpos, float diameterpos, float minChargediameterpos, float maxChargediameterpos) {
x = xpos;
y = ypos;
r = rpos;
g = gpos;
b = bpos;
transparency = trans;
count = countpos;
diameter = diameterpos;
minChargediameter = minChargediameterpos;
maxChargediameter = maxChargediameterpos;
}
void move() {
if (((dist(mouseX, mouseY, x, y))<(diameter/2))&&(mousePressed == true)) {//tests to see if mouse is on charge
if (mouseButton == LEFT) {
x = mouseX;
y = mouseY;
}
else if (mouseButton == RIGHT) {//dragging mouse toward centre decreases size of charge
if (((dist(mouseX, mouseY, x, y))-(dist(pmouseX, pmouseY, x, y)))>(0)) {
if (diameter < maxChargediameter) {//prevents diameter getting too big, max diameter is 400
diameter = diameter + 5; //increases size of diameter
transparency += 0.5;
}
} //dragging mouse away from center increases size of charge
else if (((dist(mouseX, mouseY, x, y))-(dist(pmouseX, pmouseY, x, y)))<(0)) {
if (diameter > minChargediameter) {//prevents diameter getting too small and circle disappearing, min diameter is 20
diameter = diameter - 5; // decreases size of diameter
transparency -= 0.5;
}
}
}
}
}
void display() {
fill(r, g, b, transparency);
ellipse(x, y, diameter, diameter);
}
void released() {
if (((dist(mouseX, mouseY, x, y))<(diameter/2))) {
count = (count+1)%2;
if (count==1) {
r = 0;
b = 255;
}
else if (count == 0) {
r = 255;
b = 0;
}
}
}
}
class Field {
float xField, yField, magnitude, theta, maxdist;
float vector;
int c, value, sign = 1;
Field(float xFieldpos, float yFieldpos, float magnitudepos, float thetapos, float maxdistpos) {
xField = xFieldpos;
yField = yFieldpos;
magnitude = magnitudepos;
theta = thetapos;
maxdist = maxdistpos;
}
void plot() {
float theta = atan((targetY-yField)/(targetX-xField));
if (targetX<xField) {
theta+=PI;
}
if (pol == 0) {
theta+=PI;
}
//////////////////////////////////////////////////////////////
float d = (sqrt(pow(targetY-yField, 2)+pow(targetX-xField, 2)));
//////////////////////////////////////////////////////////////
float maxFieldstrength, minFieldstrength = 0;
float newVectorlengthmin = 10, newVectorlengthmax = 35;
float minDiameter = 20, maxDiameter = 400;
//////////////////////////////////////////////////////////////////////////////////////////////////////
maxFieldstrength = map(fieldStrength, minDiameter, maxDiameter, newVectorlengthmin, newVectorlengthmax);
//////////////////////////////////////////////////////////////////////////////////////////////////////
if (d>maxFieldstrength) {
float magnitude = map(d, 0, maxdist, maxFieldstrength, minFieldstrength);
//////////////////// d ir maxFieldstrength - apibreztas, maxdist - 400, minFieldstrength - 0
float fieldTransparency = map(d, 0, maxdist, 205, -20);
stroke(1, fieldTransparency);
fill(0, 0, 0);
pushMatrix();
translate(xField, yField);
rotate(theta);
line(0, 0, magnitude, 0);
popMatrix();
noFill();
noStroke();
}
}
void update(float x, float y, float diameter, float minChargediameter, float maxChargediameter) {
targetX = x;
targetY = y;
fieldStrength = diameter;
minDiameter = minChargediameter;
maxDiameter = maxChargediameter;
}
void polarity(float r) {
pol = r;
}
}
1