Author Topic: Check polyline for square/rectangle  (Read 22672 times)

0 Members and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Check polyline for square/rectangle
« Reply #60 on: January 14, 2013, 07:40:29 AM »
What about equilateral triangles / pentagons / heptagons / nonagons ...  :wink:

 :-)
Code - Auto/Visual Lisp: [Select]
  1. (defun test-polygon (E FUZZ / L)
  2.                   (vl-remove-if-not (function (lambda (a) (= (car a) 10))) (entget e))
  3.           )
  4.   )
  5.               (lambda (a b c d)
  6.                 (and (equal (cos (- (angle b c) (angle a b))) (cos (- (angle c d) (angle b c))) fuzz)
  7.                      (equal (distance a b) (distance b c) fuzz)
  8.                 )
  9.               )
  10.             )
  11.             l
  12.             (append (cdr l) l)
  13.             (append (cddr l) l)
  14.             (append (cdddr l) l)
  15.   )
  16. )
« Last Edit: January 14, 2013, 07:45:01 AM by ElpanovEvgeniy »

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Check polyline for square/rectangle
« Reply #61 on: January 14, 2013, 07:52:03 AM »
Nice Evgeniy  :-)

To offer an alternative...

Code - Auto/Visual Lisp: [Select]
  1. (defun regularpolygon-p ( e / f l )
  2.     (defun f ( l )
  3.         (or (null (cdddr l))
  4.             (and
  5.                 (equal
  6.                     (vxv (mapcar '- (car   l) (cadr   l))
  7.                          (mapcar '- (cadr  l) (caddr  l))
  8.                     )
  9.                     (vxv (mapcar '- (cadr  l) (caddr  l))
  10.                          (mapcar '- (caddr l) (cadddr l))
  11.                     )
  12.                     1e-8
  13.                 )
  14.                 (f (cdr l))
  15.             )
  16.         )
  17.     )
  18.     (and
  19.         (cddr (setq l (apply 'append (mapcar '(lambda ( x ) (if (= 10 (car x)) (list (cdr x)))) (entget e)))))
  20.         (f (cons (last l) l))
  21.     )
  22. )
  23.  
  24. ;; Vector Dot Product  -  Lee Mac
  25. ;; Args: u,v - vectors in R^n
  26.  
  27. (defun vxv ( u v )
  28.     (apply '+ (mapcar '* u v))
  29. )

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: Check polyline for square/rectangle
« Reply #62 on: January 14, 2013, 10:21:49 AM »
Alternative method (the most accurate)  :-)

« Last Edit: January 14, 2013, 10:27:18 AM by GP »

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Check polyline for square/rectangle
« Reply #63 on: January 14, 2013, 10:58:18 AM »
Alternative method (the most accurate)  :-)

 :-D

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Check polyline for square/rectangle
« Reply #64 on: January 14, 2013, 11:05:01 AM »

Nice Evgeniy  :-)

To offer an alternative...
I was thinking of something similar. Your routine works for any regular polygon, including star shape, but fails for rectangles and for any shape containing exclusively right angles, because dot_product is 0.0 for perpendicular vectors.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Check polyline for square/rectangle
« Reply #65 on: January 14, 2013, 11:52:50 AM »
Nice Evgeniy  :-)

To offer an alternative...
I was thinking of something similar. Your routine works for any regular polygon, including star shape, but fails for rectangles and for any shape containing exclusively right angles, because dot_product is 0.0 for perpendicular vectors.

Good catch Stefan - and there I was thinking I had a shortcut for checking both angle & length...

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Check polyline for square/rectangle
« Reply #66 on: January 15, 2013, 03:21:57 AM »
I was thinking of something similar. Your routine works for any regular polygon, including star shape, but fails for rectangles and for any shape containing exclusively right angles, because dot_product is 0.0 for perpendicular vectors.

 :-)
square of is a regular polyhedron
rectangle, this is the wrong polyhedron

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Check polyline for square/rectangle
« Reply #67 on: January 15, 2013, 03:35:52 AM »
I was thinking of something similar. Your routine works for any regular polygon, including star shape, but fails for rectangles and for any shape containing exclusively right angles, because dot_product is 0.0 for perpendicular vectors.

 :)
square of is a regular polyhedron
rectangle, this is the wrong polyhedron
Exactly what I was referring to here
Now: I'm unsure what to do since the function name clearly states it should be regular polygons, but a rectangle is strictly speaking not "regular". So should consecutive vectors' lengths also be checked? That's not actually what the OP asked for is it?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Check polyline for square/rectangle
« Reply #68 on: January 15, 2013, 04:07:48 AM »
If the program needs to find rectangles, what about the other polygon with equal angles, for example:

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Check polyline for square/rectangle
« Reply #69 on: January 15, 2013, 05:13:23 AM »
I was thinking of something similar. Your routine works for any regular polygon, including star shape, but fails for rectangles and for any shape containing exclusively right angles, because dot_product is 0.0 for perpendicular vectors.

 :-)
square of is a regular polyhedron
rectangle, this is the wrong polyhedron
You are right, a rectangle is not a regular polyhedron.
That's why I said that Lee's function fails, because it should return nil for rectangles, but it returns True.