Complete graph

edited August 2017 in How To...

Any idea on how to code complete graphs where, ellipses will work as vertices?


  • Elaborate

  • It's not hard.

    int COUNT = 7;
    final int RAD = 300;
    float gX(float a){
      return( RAD * cos( a * TWO_PI / COUNT ) );
    float gY(float a){
      return( RAD * sin( a * TWO_PI / COUNT ) );
    void setup(){
    void draw(){
      translate( width/2, height/2);
      for(int i = 0; i < COUNT; i++){
        for(int j = 0; j<i; j++){
          line(gX(i), gY(i), gX(j), gY(j));
      for(int i = 0; i < COUNT; i++){
        ellipse( gX(i), gY(i), 20, 20);
    void mousePressed(){
      if( mouseButton == LEFT ){ COUNT--; }
      if( mouseButton == RIGHT ){ COUNT++; }
      if( COUNT < 0 ) { COUNT = 0; }
  • Very nice sketch, @TfGuy44.

    @tonmoy0010 -- if that was all you wanted, looks like your problem is solved! For more heavy-duty applications there are also several Java graph libraries that you could use in Processing -- for example, GraphStream. But if you are drawing complete graphs then I'm assuming that what matters is the final shape and you don't care about labeling nodes or edges, searching, animating etc. etc.

  • Homework again?

  • Yes :p Actually we have to use arrays and put ellipses. Those ellipses will work as vertices for forming the 'complete graph'. the ellipses can be dragged along the screen and thus the graph will also change. Kind of needed it like that. @jeremydouglass @TfGuy44

  • Use of mousePressed, mouseDragged, mouseReleased, mouseClicked

  • Yes. The idea is that you should show your attempt and tell us where you're stuck. Nobody would do it for you since that would be cheating.

    Post your entire code of your own attempt.


  • Ah -- definitely don't use GraphStream if this is homework.

    If the vertices can be clicked and dragged, it sounds like each one needs to store its own coordinates, and have methods for responding to mouse events... so it sounds like perhaps your vertices are either items in an array kept by a graph object, or separate objects of the same class...?

    Of course, that depends on whether you are using objects in your course right now.

  • Change the sketch from your other discussion to have arrays for x and y

  • edited August 2017


  • Why? It's like in your other discussion

    Check in a for loop which ellipse has been clicked on

  • can you help me with part of taask B?

  • homework

    show your attempt, ask something related to a line of your own code

Sign In or Register to comment.