[please] help with vectors!
in
Programming Questions
•
1 year ago
guys, i have this sketch as a simpler test to my work in progress, this is what i need:
-agents that move in a certain direction from the center
- vectors between the final positions of each agent, drawing the polygon that they form (i can not simply make lines, because later i will have to work with the vectors)
just for testing my vectors i did lines using the coordinates of them and they are completely crazy, instead of really drawing the shape of the polygon in red...
can somebody please,please help me??
- int number = int(random(4, 10));
- Agent [] agenten = new Agent[number];
- void setup() {
- size(500, 500);
- smooth();
- noFill();
- float ang = TWO_PI / number;
- //CREATES AGENTS
- for (int i=0; i<agenten.length; i++) {
- float dx = random(cos(ang*i), cos(ang*i+1));
- float dy = random(sin (ang*i), sin (ang*i+1));
- agenten[i] = new Agent(width/2, height/2, dx, dy);
- }
- }
- void draw() {
- background(255);
- stroke(0);
- noFill();
- int time= millis();
- int timelimit= 600;
- PVector [] vec = new PVector [number] ;
- for (int i=0; i<agenten.length; i++) {
- agenten[i].render();
- if (time < timelimit) {
- agenten[i].move();
- }
- }
- //CREATES VECTORS
- for (int i=0; i < vec.length-1; i++) {
- vec [i] = new PVector (agenten[i+1].position.x-agenten[i].position.x, agenten[i+1].position.y-agenten[i].position.y);
- }
- vec[vec.length-1] = new PVector (agenten[vec.length-1].position.x - agenten[0].position.x, agenten[vec.length-1].position.y - agenten[0].position.y);
- println(vec);
- beginShape();
- noStroke();
- fill(255,0,0,50);
- for (int i = 0; i < agenten.length; i++) {
- vertex(agenten[i].position.x,agenten[i].position.y);
- }
- endShape(CLOSE);
- // CREATES LINES FROM ONE TO THE OTHER VECTOR
- stroke(0);
- translate (width/2, height/2);
- for (int i=0; i<vec.length-1; i++) {
- line (vec[i+1].x, vec[i+1].y, vec[i].x,vec[i].y);
- }
- line (vec[vec.length-1].x, vec[vec.length-1].y, vec[0].x,vec[0].y);
- }
- //AGENT
- class Agent {
- PVector position;
- PVector direction;
- int lim = 400;
- Agent (float theX, float theY,float dx,float dy) {
- position = new PVector (theX, theY);
- direction = new PVector (dx, dy);
- }
- void render() {
- for (int i=0; i<number;i++){
- fill(255,0,0);
- ellipse(position.x, position.y,1,1);
- }
- }
- void move() {
- direction.normalize();
- direction.mult(random(20));
- position.add(direction);
- }
- }
1