Author Topic: Is a number within a range?  (Read 1661 times)

0 Members and 1 Guest are viewing this topic.

Daniel J. Ellis

  • Swamp Rat
  • Posts: 811
Is a number within a range?
« on: November 12, 2009, 03:53:48 AM »
I am working on a routine (or rather a part of a routine) that figures out whether a text object is inside or outside of a polyline.

I'm doing it by extracting the text's insertion point into a variable (two, actually, x and y points) and the polyline into a list of its vertices.

I then cycle through each vertex in turn, seeing whether the x-value of the text point lies between those of the two points.  If it is I can check the y-coordinates, if it's not just pass onto the next point.

The routine I've developed to do this comparison is this:
Code: [Select]
(DEFUN lessismore ( x y z / )

(IF
(>= x y)
(IF
(<= x z)
1;(PRINC "\nx LIES BETWEEN y AND z")
0;(PRINC "\nx LIES OUTSIDE THE RANGE")
);IF
0;(PRINC "\nx LIES OUTSIDE THE RANGE")
);IF
(PRINC)
)

x, y, and z are the point of the object, and of the first, and second vertex, respectively.

My first test run, the insertion point (x) is 7720.1, the first vertex point (y) is 7723.28, and the second vertex point (z) is 7720.84, i.e. the second vertex is *before* the first.

You and I can easily see that x is indeed within the range, the routine, though, can't, since it it sees the insertion point as being above the frist vertex.

The only way, so far, that I can think of to get round this would require just over twice the amount of code (compare the two vertices, then have two options, either as written above, or as above with the "y"s and "z"s swapped).

Can anybody suggest a cleaner way to do it?

dJE
===
dJE

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is a number within a range?
« Reply #1 on: November 12, 2009, 05:15:19 AM »
I am working on a routine (or rather a part of a routine) that figures out whether a text object is inside or outside of a polyline.

I'm doing it by extracting the text's insertion point into a variable (two, actually, x and y points) and the polyline into a list of its vertices.

I then cycle through each vertex in turn, seeing whether the x-value of the text point lies between those of the two points.  If it is I can check the y-coordinates, if it's not just pass onto the next point.

The routine I've developed to do this comparison is this:
Code: [Select]
(DEFUN lessismore ( x y z / )

(IF
(>= x y)
(IF
(<= x z)
1;(PRINC "\nx LIES BETWEEN y AND z")
0;(PRINC "\nx LIES OUTSIDE THE RANGE")
);IF
0;(PRINC "\nx LIES OUTSIDE THE RANGE")
);IF
(PRINC)
)

x, y, and z are the point of the object, and of the first, and second vertex, respectively.

My first test run, the insertion point (x) is 7720.1, the first vertex point (y) is 7723.28, and the second vertex point (z) is 7720.84, i.e. the second vertex is *before* the first.

You and I can easily see that x is indeed within the range, the routine, though, can't, since it it sees the insertion point as being above the frist vertex.

The only way, so far, that I can think of to get round this would require just over twice the amount of code (compare the two vertices, then have two options, either as written above, or as above with the "y"s and "z"s swapped).

Can anybody suggest a cleaner way to do it?

dJE

Quote
that figures out whether a text object is inside or outside of a polyline.

Do you mean the Text Object or the Text Insert Point. ??

Is the between thingy part of your attempt to determine if a point is inside a closed polyline ??

Search the site for code by (from memory) CAB and Joe Bourke titles something like IsInside or something similar.


If you want to test the Text Object you'll need to get the bounding Box and test each corner ..... but you will strike issues like text Rotation, etc which may require some special treatment. 


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

fixo

  • Guest
Re: Is a number within a range?
« Reply #2 on: November 12, 2009, 06:34:22 AM »
I am working on a routine (or rather a part of a routine) that figures out whether a text object is inside or outside of a polyline.

I'm doing it by extracting the text's insertion point into a variable (two, actually, x and y points) and the polyline into a list of its vertices.

I then cycle through each vertex in turn, seeing whether the x-value of the text point lies between those of the two points.  If it is I can check the y-coordinates, if it's not just pass onto the next point.

The routine I've developed to do this comparison is this:
Code: [Select]
(DEFUN lessismore ( x y z / )

(IF
(>= x y)
(IF
(<= x z)
1;(PRINC "\nx LIES BETWEEN y AND z")
0;(PRINC "\nx LIES OUTSIDE THE RANGE")
);IF
0;(PRINC "\nx LIES OUTSIDE THE RANGE")
);IF
(PRINC)
)

x, y, and z are the point of the object, and of the first, and second vertex, respectively.

My first test run, the insertion point (x) is 7720.1, the first vertex point (y) is 7723.28, and the second vertex point (z) is 7720.84, i.e. the second vertex is *before* the first.

You and I can easily see that x is indeed within the range, the routine, though, can't, since it it sees the insertion point as being above the frist vertex.

The only way, so far, that I can think of to get round this would require just over twice the amount of code (compare the two vertices, then have two options, either as written above, or as above with the "y"s and "z"s swapped).

Can anybody suggest a cleaner way to do it?

dJE

Try this example
Code: [Select]
(defun c:demo (/ elist en ptlist ss txtss)
  (princ "\nSelect a boundary")
  (setq ss (ssget "_:S:E"
      (list
(cons -4 "<AND")
(cons 0 "LWPOLYLINE")
(cons 8 "PAVEMENT") ; change layer
;;; (cons 62 6) ; change color
(cons 70 1) ; flag (1 - closed polyline)
(cons -4 "AND>")
      )
       )
      )

(setq en (ssname ss 0))
(setq
  ptlist (vl-remove-if
   (function not)
   (mapcar
     (function (lambda (x)
(if (= 10 (car x))
   (list (car (cdr x)) (cadr (cdr x)))
)
       )
     )
     (entget en)
   )
)
)

(setq txtss (ssget "_CP"
   ptlist
   (list
     (cons -4 "<AND")
     (cons 0 "TEXT")
     (cons 8 "AREA_OVERALL") ; change layer
     (cons 7 "Standard"); change text style
     (cons 40 2.1) ; change text height
     (cons -4 "AND>")
   )
    )
)
     
      (while (setq en (ssname txtss 0))
(setq elist (entget en))
;; do whatever you need
;; i.e. change layer
(entmod (subst  (cons 8 "0")(assoc 8 elist) elist))
(ssdel en txtss)
)

  (princ)
)

~'J'~

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Is a number within a range?
« Reply #3 on: November 12, 2009, 08:21:54 AM »
No time this AM...but
John Uhden thread


>DownLoad<
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

uncoolperson

  • Guest
Re: Is a number within a range?
« Reply #4 on: November 12, 2009, 10:39:19 AM »
would ssget wp or cp, based off the polyline work?


edit: sorry fixo, i didn't look at your example
« Last Edit: November 12, 2009, 11:38:58 AM by uncoolperson »

Daniel J. Ellis

  • Swamp Rat
  • Posts: 811
Re: Is a number within a range?
« Reply #5 on: November 12, 2009, 11:26:12 AM »
That is a stroke of genius, true thinking outside the box (or perhaps inside it lol).

The worst thing is that I've used that get around for a routine before! <shakes head>

Thanks UCB and fixo.

CAB - thanks for the link - I'll give it a proper read later, but go with the SSGET-WP solution, I think.

dJE
===
dJE