middle point between 2 points
in
Programming Questions
•
1 year ago
Hi,
I'm a Processing beginner.
I'm trying to draw some neurones.
These neurones will have a shape created with bezier curves. I'd like to curve the neurone at the middle of two branches. But before, I have to find the mid points.
I have a problem finding the mid points. The result's not good although I already have the angle and the distance between the two points.
If anyone can help me, it would be fine: here is the code for the moment:
Thanks!
I'm a Processing beginner.
I'm trying to draw some neurones.
These neurones will have a shape created with bezier curves. I'd like to curve the neurone at the middle of two branches. But before, I have to find the mid points.
I have a problem finding the mid points. The result's not good although I already have the angle and the distance between the two points.
If anyone can help me, it would be fine: here is the code for the moment:
- void dessinerNeurone(int x, int y, float[] extremitesX, float[] extremitesY) {
- float shrink = random(0, 0.2);
- pushMatrix();
- translate(x, y);
- strokeWeight(10);
- stroke(255);
- point(0, 0); // center point
- for (int i = 0; i < extremitesX.length - 1; i++) {
- println(i);
- float xStart = extremitesX[i];
- float yStart = extremitesY[i];
- float xStop = extremitesX[i+1];
- float yStop = extremitesY[i+1];
- float a = atan2(xStop - xStart, yStop - yStart); //println(degrees(a));
- float d = dist(xStart, yStart, xStop, yStop); //println(d);
- pushMatrix();
- translate(xStart, yStart);
- stroke(0, 255, 0);
- strokeWeight(5);
- fill(255);
- text(str(i), 0, 0); //startPoint
- point(0, 0);
- stroke(255, 0, 0);
- fill(255);
- text(str(i), (d/2*cos(a))/2, (d/2*sin(a))/2);
- point((d/2*cos(a))/2, (d/2*sin(a))/2); // middle point
- popMatrix();
- }
- popMatrix();
- }
- void setup() {
- colorMode(RGB, 255);
- size(500, 500);
- background(0);
- smooth();
- fill(0);
- int step = 200;
- for (int y = 0; y <= height; y += step) {
- for (int x = 0; x <= width; x += step) {
- float w = random(20, 30); //w = 30f;
- int n = (int) random(3, 8); //n = 6;
- float ang = 0;
- float[] extremitesX = new float[0];
- float[] extremitesY = new float[0];
- for (int i = 0; i < n ; i++) {
- float stepInc = 2*PI/n;
- float inc = stepInc;
- //float inc = random(stepInc - degrees(30), stepInc);
- strokeWeight(3);
- point(cos(ang)*w, sin(ang)*w);
- extremitesX = append(extremitesX, cos(ang)*w);
- extremitesY = append(extremitesY, sin(ang)*w);
- ang += inc;
- }
- dessinerNeurone(x, y, extremitesX, extremitesY);
- }
- }
- }
Thanks!

1