jit.lcd — How determine if a trapezoid contains an x,y pair?

Dec 15, 2006 at 7:54pm

jit.lcd — How determine if a trapezoid contains an x,y pair?

I need to know how to determine if a specific x,y coordinate exists within a trapezoid defined in a jit.lcd object.

My guess I can get this done with the right formula in an expr object, but I’m wondering if there’s a more “Max” way to do it.

I have a jit.lcd that’s 640×480.

Within that, I have a trapezoid. It will be different shape (i.e., made of 4 different points) depending on different circumstances.

So, let’s say within my 640×480 jit.lcd I have trapezoid defined by the 4 x,y coordinates:

22, 301
40, 419
35, 200
24, 150

Any easy way to determine if another x,y coordinate falls inside this trapezoid (or, conversely, falls outside of it)?

If nothing easy, anyone know the formula(s) I need to use in expr?

Thanks for any help.

Adam

#29246
Dec 15, 2006 at 11:29pm

I found a solution that works for me:

getpixel reports the color of each pixel in jit.lcd. I can adapt my program so my trapezoid is against a solid black background. So, using counters, I can use getpixel to report the color of each pixel in jit.lcd, and I can then store each non-black pixel, which is defines the set of pixels within the trapezoid.

This is a limited solution — It would be shaky if my trapezoid were written on top of an image. Even if I do the opposite logic and only report on the exact color of the trapezoid, it’s likely an image would have at least some pixels the same color as the trapezoid.

Would be happy to hear if anyone has better ideas.

Adam

#90908
Dec 16, 2006 at 1:34am

> This is a limited solution — It would be shaky if my trapezoid were
> written on top of an image. Even if I do the opposite logic and only
> report on the exact color of the trapezoid, it’s likely an image would
> have at least some pixels the same color as the trapezoid.
>
> Would be happy to hear if anyone has better ideas.
>
> Adam

forgot what this algorithm is called but it can deal with any shapes
defined as xy pairs.
be warned – quick hack and not thoroughly tested…

#N comlet within [0/1];
#P outlet 46 403 15 0;
#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P comment 35 58 94 196617 location as xy pair;
#P message 71 34 37 196617 10 10;
#P message 109 34 43 196617 29 250;
#P message 300 35 151 196617 22 301 40 419 35 200 24 150;
#P newex 46 373 27 196617 % 2;
#N counter;
#X flags 0 0;
#P newobj 189 314 66 196617 counter;
#P comment 132 248 397 196617 (((y>=y1)&&(y=y2)&&(y
&& (x< ((x2-x1)*(y-y1)/(y2-y1)+x1));
#P newex 307 199 51 196617 zl ecils 2;
#N comlet loc [list];
#P inlet 46 74 15 0;
#N comlet bounds [list];
#P inlet 278 74 15 0;
#P newex 252 137 47 196617 zl iter 2;
#P newex 252 118 40 196617 t l l 0;
#P window linecount 2;
#P newex 189 283 281 196617 if ((($i6>=$i2)&&($i6< $i4)) ||
(($i6>=$i4)&&($i6< $i2))) &&
($i5< (((($i3-$i1)*($i6-$i2))/($i4-$i2))+$i1)) then bang;
#P window linecount 1;
#P comment 132 263 85 196617 x1 y1 x2 y2 x y;
#P newex 46 354 27 196617 i;
#P newex 46 149 40 196617 t b b l;
#P newex 252 99 36 196617 zl reg;
#P newex 76 177 40 196617 unpack;
#P newex 348 225 40 196617 unpack;
#P newex 252 156 27 196617 t l l;
#P comment 264 58 94 196617 bounds as xy pairs;
#P number 94 399 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 376 64 48 196617 loadbang;
#P connect 14 0 7 0;
#P connect 20 0 7 0;
#P connect 21 0 7 0;
#P connect 7 0 8 0;
#P connect 8 0 18 0;
#P connect 18 0 23 0;
#P connect 17 0 8 1;
#P connect 7 2 5 0;
#P connect 18 0 1 0;
#P connect 3 1 10 0;
#P connect 10 0 17 0;
#P connect 11 2 17 3;
#P connect 7 1 6 0;
#P connect 6 0 11 0;
#P connect 11 0 12 0;
#P connect 12 0 3 0;
#P connect 19 0 6 1;
#P connect 13 0 6 1;
#P connect 4 0 10 2;
#P connect 0 0 19 0;
#P connect 11 1 15 0;
#P connect 15 1 4 0;
#P connect 3 0 4 0;
#P connect 4 1 10 3;
#P connect 5 0 10 4;
#P connect 5 1 10 5;
#P window clipboard copycount 24;

#|
fredrikolofsson.com klippav.org musicalfieldsforever.com
|#

#90909
Dec 16, 2006 at 1:41pm

Thanks, I’ll check it out.

Adam

#90910

You must be logged in to reply to this topic.