I want to display ID of an object in its centre

edited June 2016 in Hello Processing

I am trying to display "id" in the centre of object.

Niche(int id, PVector loc, boolean settled) {
  ref = id;  
  happy = settled;  
  //create Nucleus
  centre = new Nucleus(id,  false, loc);
  //create boundaryNodes
  for (int i=0; i<numNodes; i++) {
    origin = loc.get();
    nodes[i] = new boundaryNode(id, true, false, origin);

Thanks in Advance.

Untitled

Answers

  • add up all the x and y coordinates, divide by the count to give you the centre of the shape.

    post a runnable example.

  • i have center location nucleus.add(space[i].nodes[i].pos); but i want to display number or string inside it.

  • edited June 2016
    //nucleus is the centre of the niche
    
    class Nucleus {
    
      int id;
      PVector pos;
      PVector nucleus = new PVector();
      boolean frozen;
    
      Nucleus(int identity, boolean hunger, PVector loc) {
        id = identity;
        pos = loc.get();
      }
    
      void run() {
        updatePos();
        render();
      }
    
      void updatePos() {
    //   println(id, "current pos", pos);
        nucleus = new PVector();
        for (int i=0; i<space.length; i++) {
          if (space[i].ref == id) {
            for (int j=0; j<space[i].nodes.length; j++) {
              nucleus.add(space[i].nodes[i].pos);
            }
          }   
        }
        nucleus.div(numNodes);
    // println("current nucleus", nucleus);
    //   float d = pos.dist(nucleus);
    //    if (d == 0) {
    //      frozen = true;
    //      println("FROZEN .........!!!!!!!!!!!!!!!");
    //    }
        pos = nucleus;
    //    println(id, "new pos", pos);
      }
    
      void render() {
        stroke(200, 70);
    //    stroke(250, 50);
        noFill();
        ellipse(pos.x, pos.y, 8, 8);
        stroke(10);
        point(pos.x, pos.y);
      }
    }
    
  • edited June 2016
    • for ellipse() you can change the meaning of the parameters with ellipseMode

    • for text() you can change it with textAlign. e.g textAlign(CENTER,CENTER); or textAlign(LEFT);

    so

    void render() { 
    
        stroke(200, 70); 
        // stroke(250, 50); 
        noFill(); 
        ellipse(pos.x, pos.y, 8, 8); 
        stroke(10); 
        point(pos.x, pos.y); 
    
        fill(0); 
        textAlign(CENTER,CENTER);
        text(id,pos.x, pos.y); 
    } 
    
Sign In or Register to comment.