Skip to content

Тема ООП не раскрыта :-) #1

@XRay3D

Description

@XRay3D

Забавная среда для поделий и прототипов.
Накидал немного ООП.

int rad = 200;
int amount = 20;

class Dot { 
  float x, y, velX, velY, r;
  color col;
  int idx;
  Dot(int i) { 
    idx = i;
    r = random(5, 10);
    x = random(r, width  - r);
    y = random(r, height - r);
    velX = random(2, 6) * ((int)random(0, 2) == 0 ? +1 : -1);
    velY = random(2, 6) * ((int)random(0, 2) == 0 ? +1 : -1);
    col = color(random(0, 255), random(0, 255), random(0, 255));
  }

  void updateVel() {    
    x += velX;    
    y += velY; 
    if (x < r || x > width  - r) velX = -velX;
    if (y < r || y > height - r) velY = -velY;
  }  

  void drawCircle() {    
    noStroke();
    fill(0);
    circle(x, y, r * 2);
  }

  void drawLine(Dot d) {    
    float distance = dist(x, y, d.x, d.y);
    if (distance < rad) {
      strokeWeight(map(distance, 0, rad, 4, 0.2));
      line(x, y, d.x, d.y);
    }
  }
} 

Dot dots[] = new Dot[amount];

void setup() {
  size(800, 600, P3D);
  smooth(8);
  colorMode(HSB, 255, 255, 255);
  for (int i = 0; i < amount; i++)   
    dots[i] = new Dot(i);
}

void draw() {
  background(200); 
  stroke(0);

  for (Dot d : dots) 
    d.updateVel();

  for (Dot d : dots) {
    for (int j = 0; j < d.idx; j++) {
      d.drawLine(dots[j]);
    }
  }

  for (Dot d : dots) 
    d.drawCircle();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions