How can I know if a text intersects another text?

edited September 2014 in Programming Questions

Hello! I am new to this forum, so excuse me if I do not give enough information at once. I am trying to make a word cloud like Wordle. I am halfway through (I already have the words), but I do not know how to check if a text intersects another one, since I am also rotating the text. Any idea?

Thank you.

Answers

  • edited September 2014

    Well first of all one easy thing could be keeping the words all the same size. From their it will be a check and adjust process where you run it, check it, than adjust. How would you know, without checking? I can't really say, just consider how long each word is and size it depending on that.for example.

    void setup(){
      size(200,200);
    }
    
    void draw(){
      textsize(10);
      text("long text",50,10);
      textsize(20);
      text("short",50,30);
    }
    

    See how I added 20 on the "y" value of the second text? I was just making sure they had room. But if I wanted to make it spin, I would have to make it at least 40 or 50 in the y value, to ensure they didn't touch if they were spinning.

    Hope it helped! Techwiz777

  • Thanks for answering! I would like to know more or less how to do it automatically. I thought on making an invisible rectangle covering the word, so the coordinates are easier to manage. Isn't there any library to check intersection? Thanks again!

  • edited September 2014
  • but I do not know how to check if a text intersects another one, since I am also rotating the text

    It is very simple to test for the intersection between 2 rectangles provided they are not rotated (have a look at page 4 of the Geometry 2D Cookbook)

    Once you rotate the them then you have a problem. You could assume that each word is inside a circle whose diameter is given by the textWidth() because it is easier to test for circle-circle intersection, but that might give unnatural separation in the text Y direction.

    Alternatively you could test each side of the rotated rectangle with each side of the second rotated rectangle using simple line-line intersection.

    I am not aware of any Processing V2+ compatible libraries for doing this, but someone else might.

  • _vk_vk
    edited September 2014

    Another way of doing collision detection is to draw whatever is drawn in main screen in a second "layer"(PGraphics). Then you check the color of desired position in this never displayed layer, to see if "that place is taken" : ). But I don't know if this can be of help here as I believe you still need to handle rotated coordinates. And check a all rect coordinates, what might be just as hard.

    There is two typography libraries linked from library page, you might check if they can help you geomerative perhaps

    http://processing.org/reference/libraries/#typography

  • Answer ✓

    Thank you very much!(including the bouncing words... quite interesting:) Geomerative is the library i was looking for. Thanks everyone!

Sign In or Register to comment.