Drawing a grid
in
Programming Questions
•
10 months ago
Hello,
I would like to draw lines between points in order to create a grid.
The problem is I cannot access each coordinate of the points.
The part of script that is marked in orange colors shows that the location of a point is equal to magnitude.
But as I image it works then you connect first point with second, second with third point, but here I have only magnitude value.
I thought, that in order to make a grid, you can evalute distance between points and connect the closest ones, but still there is only magnitude value.... Ech ech ech...
How make just a simple grid?
Please help me:)
I would like to draw lines between points in order to create a grid.
The problem is I cannot access each coordinate of the points.
The part of script that is marked in orange colors shows that the location of a point is equal to magnitude.
But as I image it works then you connect first point with second, second with third point, but here I have only magnitude value.
I thought, that in order to make a grid, you can evalute distance between points and connect the closest ones, but still there is only magnitude value.... Ech ech ech...
How make just a simple grid?
Please help me:)
- int numCharges = 1;
Electrons[] charge = new Electrons[numCharges];//ATTRACTORS
int numVectorsX = 21;
int numVectorsY = 21;//NUMBER OF POINTS
int distance = 30;//GAPS
Field[][] vectors = new Field[numVectorsX][numVectorsY];//GRIDAS
float targetX = 300, targetY = 150, pol, fieldStrength, minDiameter, maxDiameter;
float theta = 0;//angle between charge and each vector
int magnitude;//LOCATION
int r, g, b;
float Fieldstrength;
void setup() {
size(620, 620);
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
charge[k] = new Electrons(100 + (200*k), 100 + (50*k), 0, 0, 0, 100, 1, 100, 20, 400); //ATTRACTOR POINT
}
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, 50);
rect(0, 0, 800, 800);
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 k = 0; k < numCharges; k++) {
for (int i = 0; i < numVectorsX; i++) {
for (int j = 0; j < numVectorsY; j++) {
// xField, yField, l, theta
vectors[i][j].plot();
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'.
}
}
}
}
void mouseReleased() {
if (mouseButton == CENTER) {
for (int k = 0; k < numCharges; k++) {
charge[k].released();
}
}
}
- class Electrons {
float x, y, r, g, b, transparency, diameter, minChargediameter, maxChargediameter;
int count;
//Constructor
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);//fills the charge with colours red, green, blue
ellipse(x, y, diameter, diameter);//draws the charge
}
void released() {
if (((dist(mouseX, mouseY, x, y))<(diameter/2))) {//tests to see if mouse is on charge
count = (count + 1) % 2;
if (count == 1) {//sets colour to blue once
r = 0;
b = 255;
}
else if (count == 0) {//set colour red to blue
r = 255;
b = 0;
}
}
}
}
class Field {
float xField, yField, magnitude, theta, maxdist;
float vector;
int c, value, sign=1;
//Constructor
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);
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);
vector = magnitude;
if (frameCount0 == 0) {
}
c=frameCount0;
if (c==0) sign=-sign;
value=c*sign;
if (value!=0)
vector = magnitude + value;
ellipse(vector, 0, 10, 10);
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