Author Topic: ==={challenge}=== Word Puzzle  (Read 18370 times)

0 Members and 2 Guests are viewing this topic.

pBe

  • Bull Frog
  • Posts: 402
==={challenge}=== Word Puzzle
« on: May 04, 2012, 12:36:35 PM »
Find these words and cross out with a line using the attached drawing file
"BOX"
"MAD"
"AMASS"
"LSP"
"EGG"
"AXE"
"ATTIC"
"GLAD"

I dont have a code yet. its just a spur of the moment thingy  :-D
Using a code of course .

I'l ltry to wirte one tonigt. and check in tommorow
« Last Edit: May 04, 2012, 01:29:12 PM by pBe »

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #1 on: May 04, 2012, 01:54:19 PM »
My entry:

Code - Auto/Visual Lisp: [Select]
  1. ;; Solve Word Puzzle  -  Lee Mac
  2. ;; For Swamp Challenge:
  3. ;; http://www.theswamp.org/index.php?topic=41649.0
  4.  
  5. (defun c:solvewordpuzzle ( / e i l s words )
  6.  
  7.     (setq words
  8.        '(
  9.             "BOX"
  10.             "MAD"
  11.             "AMASS"
  12.             "LSP"
  13.             "EGG"
  14.             "AXE"
  15.             "ATTIC"
  16.             "GLAD"
  17.         )
  18.     )
  19.    
  20.     (if (setq s (ssget "_X" '((0 . "TEXT") (72 . 1) (73 . 2))))
  21.         (progn
  22.             (repeat (setq i (sslength s))
  23.                 (setq e (entget (ssname s (setq i (1- i))))
  24.                       l (cons (cons (cdr (assoc 1 e)) (cdr (assoc 11 e))) l)
  25.                 )
  26.             )
  27.             (setq l
  28.                 (apply 'append
  29.                     (mapcar
  30.                         (function
  31.                             (lambda ( a )
  32.                                 (if (cdr a)
  33.                                     (list a (reverse a))
  34.                                     (list a)
  35.                                 )
  36.                             )
  37.                         )
  38.                         (append
  39.                             (setq l
  40.                                 (mapcar
  41.                                     (function
  42.                                         (lambda ( a )
  43.                                             (vl-sort a
  44.                                                 (function
  45.                                                     (lambda ( a b ) (< (cadr a) (cadr b)))
  46.                                                 )
  47.                                             )
  48.                                         )
  49.                                     )
  50.                                     (vl-sort (LM:GroupByFunction l (lambda ( a b ) (equal (caddr a) (caddr b) 0.01)))
  51.                                         (function
  52.                                             (lambda ( a b ) (> (caddar a) (caddar b)))
  53.                                         )
  54.                                     )
  55.                                 )
  56.                             )
  57.                             (setq l (apply 'mapcar (cons 'list l)))
  58.                             (_getdiagonals l)
  59.                             (_getdiagonals (mapcar 'reverse l))
  60.                         )
  61.                     )
  62.                 )
  63.             )
  64.             (foreach word words
  65.                 (vl-some
  66.                     (function
  67.                         (lambda ( a / p )
  68.                             (if (setq p (vl-string-search word (apply 'strcat (mapcar 'car a))))
  69.                                 (entmake
  70.                                     (list
  71.                                        '(0 . "LINE")
  72.                                         (cons 10 (cdr (nth p a)))
  73.                                         (cons 11 (cdr (nth (+ p (1- (strlen word))) a)))
  74.                                        '(62 . 3)
  75.                                     )
  76.                                 )
  77.                             )
  78.                         )
  79.                     )
  80.                     l
  81.                 )
  82.             )
  83.         )
  84.     )
  85.     (princ)
  86. )
  87.  
  88. (defun _getdiagonals ( m / i l )
  89.     (setq l (1- (length m))
  90.           i -1
  91.     )
  92.     (mapcar (function (lambda ( x ) (apply 'append x)))
  93.         (apply 'mapcar
  94.             (cons 'list
  95.                 (mapcar
  96.                     (function
  97.                         (lambda ( a )
  98.                             (setq a (mapcar 'list a))
  99.                             (repeat (- l (setq i (1+ i))) (setq a (cons nil a)))
  100.                             (setq a (reverse a))
  101.                             (repeat i (setq a (cons nil a)))
  102.                             (reverse a)
  103.                         )
  104.                     )
  105.                     m
  106.                 )
  107.             )
  108.         )
  109.     )        
  110. )
  111.  
  112. ;; Group By Function  -  Lee Mac
  113. ;; Groups items considered equal by a given predicate function
  114.  
  115. (defun LM:GroupByFunction ( lst fun / tmp1 tmp2 x1 )
  116.     (if (setq x1 (car lst))
  117.         (progn
  118.             (foreach x2 (cdr lst)
  119.                 (if (fun x1 x2)
  120.                     (setq tmp1 (cons x2 tmp1))
  121.                     (setq tmp2 (cons x2 tmp2))
  122.                 )
  123.             )
  124.             (cons (cons x1 tmp1) (LM:GroupByFunction tmp2 fun))
  125.         )
  126.     )
  127. )
  128.  

kruuger

  • Swamp Rat
  • Posts: 625
Re: ==={challenge}=== Word Puzzle
« Reply #2 on: May 04, 2012, 02:51:06 PM »
My entry:
W O W  :-o
Lee could give a short description how it works?
thanks
kruuger

LE3

  • Guest
Re: ==={challenge}=== Word Puzzle
« Reply #3 on: May 04, 2012, 03:05:14 PM »
reading the description above your avatar - indeed and true +1

bet that now you even dream with all the solutions on your mind, and can't wait to wake up > to start coding <

molto bene signore.

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #4 on: May 04, 2012, 04:55:29 PM »
W O W  :-o
Lee could give a short description how it works?
thanks
kruuger

Thanks Kruuger, you're too kind  :-)

The method is actually rather simple, print out the list variable 'l' before the foreach loop and a keen programmer such as yourself will see right through it  :-)

Of course, if you want further explanation, I would be more than happy to oblige.

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #5 on: May 04, 2012, 04:58:25 PM »
reading the description above your avatar - indeed and true +1

bet that now you even dream with all the solutions on your mind, and can't wait to wake up > to start coding <

"Needs a day job"

I admit, I've long been addicted to this programming and can't resist a challenge  8-)

molto bene signore.

Cheers Luis  :-)

KewlToyZ

  • Guest
Re: ==={challenge}=== Word Puzzle
« Reply #6 on: May 04, 2012, 05:27:26 PM »
reading the description above your avatar - indeed and true +1

bet that now you even dream with all the solutions on your mind, and can't wait to wake up > to start coding <

"Needs a day job"

I admit, I've long been addicted to this programming and can't resist a challenge  8-)

molto bene signore.

Cheers Luis  :-)

I see your stuff in all of the CAD forums lol!
Quote
(foreach word words
                (vl-some
                    (function
That messes with my head =P
« Last Edit: May 04, 2012, 05:31:45 PM by KewlToyZ »

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Word Puzzle
« Reply #7 on: May 05, 2012, 12:49:24 AM »
Incredible.
I'm not even halfway done with mine. Having trouble with diagonal search.

Trying not to look  at your code. I copeid and paste it with my eyes close  :-D

I'll post mine when i'm done.

Again. Incredible

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #8 on: May 05, 2012, 05:38:40 AM »
I see your stuff in all of the CAD forums lol!

For a short time I participated in a number of different CAD forums, ADG / AUGI, but since, in my experience, TheSwamp is so much better (and I'm not just talking about the much cleaner / clearer layout of the site, but the more friendly and jovial atmosphere we have here and an incredibly knowledgeable community too) that I now tend to spend the majority of my time here.

Quote
(foreach word words
                (vl-some
                    (function
That messes with my head =P

 :lol:
« Last Edit: May 05, 2012, 05:42:13 AM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #9 on: May 05, 2012, 05:43:10 AM »
Incredible.
I'm not even halfway done with mine. Having trouble with diagonal search.

Trying not to look  at your code. I copeid and paste it with my eyes close  :-D

I'll post mine when i'm done.

Again. Incredible

Cheers pBe, that's very kind. I look forward to seeing your alternative solution! 
Its always interesting to see how different minds tackle a problem :-)

Also, thanks for posting the Challenge, I really enjoyed writing this one.

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Word Puzzle
« Reply #10 on: May 06, 2012, 02:50:04 AM »
Cheers pBe, that's very kind. I look forward to seeing your alternative solution! 
Its always interesting to see how different minds tackle a problem :-)

Also, thanks for posting the Challenge, I really enjoyed writing this one.

Glad you like it Lee. It didt take you long to come up with a solution though  :-D
I'm still trying to figure out the diagonal search.. I have an idea how to go about it but cant put it on code yet.

Here's what i have so far.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:solveit (/ _build _HiLow stp Where is it crossed ss Olist)
  2. ;;;                                                             ;;;;
  3. (defun _build (lst_ ref mode / func)
  4. (setq Func (if (zerop mode)
  5.                (list (cadr ref) cadadr caadr)
  6.                (list (car ref) caadr cadadr)))
  7.               (vl-remove-if-not
  8.                     '(lambda (w) (eq ((cadr func) w) (car func)))
  9.                     lst_)
  10.       )
  11. (defun _HiLow  (lev lev2 lst)
  12.       (list
  13.             (apply lev (mapcar 'car lst))
  14.             (apply lev2 (mapcar 'cadr lst))
  15.             ))
  16. (defun _check (w ln / run f woot i)
  17. (while (and ln (not woot)
  18.        (setq run (member (car w)  (mapcar 'car ln))))
  19.        (if (and (>= (length run)(length w))
  20.                 (apply '= (mapcar '(lambda (k s)
  21.                                 (if (= k s)1 0)) w run )))
  22.            (foreach l w
  23.                  (setq woot (cons (assoc l ln) woot)
  24.                          ln (member (assoc l ln) ln)
  25.                          ln (vl-remove (assoc l ln) ln)))
  26.            (setq ln (if run (vl-remove (assoc (car run) ln) ln) nil))
  27.            )
  28.         ) woot
  29.       )
  30. (setq  _ReList (lambda  (lst_ num / a i)
  31.       (setq n (/ num 2)  i -1)
  32.       (setq a (member (nth n lst_) lst_))
  33.       (repeat n
  34.             (setq a (append a (list (nth (setq i (1+ i)) lst_)))))
  35.       a ))
  36. (defun _DiagonalLines ( r lst / s l)
  37. (setq s -1 _repf (lambda  (i n / x te )
  38.       (repeat r  (setq x (cons  (if (setq te (nth (setq i (1+ i))
  39.                        (nth (setq n (1+ n)) lst))) te '(0 (0.0))) x))) x))
  40. (repeat (1- r) (setq v (_repf s -1)  l (cons v l) s (1+ s)))
  41.       l)      
  42. ;;;                                                             ;;;      
  43. (setq wlist '("BOX" "MAD"  "AMASS" "LSP" "EGG" "AXE" "ATTIC" "GLAD"))
  44. (setq Olist nil
  45.       ss (ssget "_X" '((0 . "TEXT")(1 . "@"))))
  46. (repeat (setq i (sslength ss))
  47.       (setq Olist (cons (list (ascii (cdr (assoc 1 (setq e  (entget
  48.                 (ssname ss (setq i (1- i))))))))
  49.                               (cdr (assoc 11 e)))
  50.                         Olist))
  51.       )
  52. (setq Where (apply 'append
  53. (mapcar '(lambda (h / x ln)
  54.                (setq lst (eval (cadr h)) f (eval (last h)))
  55.                (while (and (setq stp (_HiLow 'min
  56.                                                'max
  57.                                                (mapcar 'cadr lst)))
  58.                            (setq x (_build lst stp (car h)))
  59.                            (setq ln (cons (vl-sort x '(lambda (x1 x2)(< (f x1)(f x2))))
  60.                                            ln))
  61.                            (foreach
  62.                                   il  x
  63.                                  (setq lst (vl-remove il lst)))))
  64.                ln)
  65.         (list '(0 Olist caadr) '(1 Olist cadadr)))
  66.                   )
  67.       )
  68. (setq row (length (cadr where))
  69.       where (apply 'append (list (_DiagonalLines row where)
  70.                                  (_DiagonalLines  row (_ReList where (length where)))
  71.                                  (cdr (_DiagonalLines  row (mapcar 'reverse where)))
  72.                                  (cdr (_DiagonalLines  row (_ReList (reverse
  73.                                         (mapcar 'reverse where))(length where))))
  74.                                   where)))
  75. (foreach wl (mapcar 'vl-string->list wlist)
  76.       (setq is Where crossed nil)
  77.       (while (and (setq it (car is))(null crossed))
  78.            (if  (setq z (if (setq y (_check wl it))
  79.                                   y (_check (reverse wl) it)))
  80.                 (setq crossed
  81.                         (entmake  (list '(0 . "LINE")
  82.                         (cons 10 (cadar z))
  83.                         (cons 11 (cadr (last z)))
  84.                         ))
  85.                         )
  86.                 )
  87.                     (setq is (cdr is))
  88.                 )
  89.       )(princ)
  90. )

Maybe later today I'll update it and post the complete code later  (hopefully)
 <<<<  I need an aspirin >>>>   :laugh:

« Last Edit: May 07, 2012, 09:21:28 AM by pBe »

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Word Puzzle
« Reply #11 on: May 07, 2012, 04:00:39 AM »
I updated the code , but it only works for even number of rows and columnc i.e 5x5 6x6....

And i'm still missing the upper left quadrant search.
I know its there somewhere , but cant quite put my finger on it.


Got it.

Code - Auto/Visual Lisp: [Select]
  1. (cdr (_DiagonalLines  row (_ReList (reverse
  2.                                         (mapcar 'reverse where))(length where))))

Also i tried a recursive version for this

Code - Auto/Visual Lisp: [Select]
  1. (defun _check (w ln / run f woot i)
  2. (while (and ln (not woot)
  3.        (setq run (member (car w)  (mapcar 'car ln))))
  4.        (if (and (>= (length run)(length w))
  5.                 (apply '= (mapcar '(lambda (k s)
  6.                                 (if (= k s)1 0)) w run )))
  7.            (foreach l w
  8.                  (setq woot (cons (assoc l ln) woot)
  9.                          ln (member (assoc l ln) ln)
  10.                          ln (vl-remove (assoc l ln) ln)))
  11.            (setq ln (if run (vl-remove (assoc (car run) ln) ln) nil))
  12.            )
  13.         ) woot
  14.       )

But i failed miserably.


CODE COMPLETED... just need 2 bottles of beer for my brain to work overtime  :-D
« Last Edit: May 07, 2012, 09:24:14 AM by pBe »

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #12 on: May 07, 2012, 10:56:58 AM »
Nice one pBe :-)

Its the Bank Holiday weekend here in the UK, so I'll take a closer look at your code sometime this week  :-)
« Last Edit: May 07, 2012, 11:01:32 AM by Lee Mac »

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Word Puzzle
« Reply #13 on: May 07, 2012, 11:11:22 AM »
Nice one pBe :-)

Its the Bank Holiday weekend here in the UK, so I'll take a closer look at your code sometime this week  :-)

Cool beans.

Enjoy the holidiay and hurry back, You guys need to school me  on recursive coding.

Cheers Lee.  :-)

I just finished disecting your code Lee

Code - Auto/Visual Lisp: [Select]

That  is definitely a better approach for checking the list for the string .
And only now that i get to understand how vl-some works.

Kudos to you . Great programming skills.
« Last Edit: May 07, 2012, 12:43:19 PM by pBe »

LE3

  • Guest
Re: ==={challenge}=== Word Puzzle
« Reply #14 on: May 07, 2012, 03:37:54 PM »
lost all my lsp powers - so -> will try to make an arx version of my own > and post the solution here <  edit: here it is...

have not been able to post any code lately so, got some time to play and did some practice - here it goes.
you can see the difference between how a task can be done in autolisp versus arx/c++ > hope that helps <

edit2: it needs to be updated for cases with different number of rows and columns >todo in a possible future<

Code - C++: [Select]
  1. [code]
  2. #include "StdAfx.h"
  3. #include "resource.h"
  4. using namespace std;
  5. using std::map;
  6. #include <algorithm>
  7. #include <vector>
  8.  
  9. bool comparePoints (const AcGePoint3d a, const AcGePoint3d b)
  10. {
  11.         if (a.x == b.x)
  12.         {
  13.                 if (a.y == b.y)
  14.                         return (a.z > b.z);
  15.                 else
  16.                         return (a.y > b.y);
  17.         }
  18.         else
  19.                 return (a.x > b.x);
  20. }
  21.  
  22. bool compare(std::pair<AcString, AcGePoint3d> pairOne, std::pair<AcString, AcGePoint3d> pairTwo)
  23. {
  24.         //return pairOne.first < pairTwo.first; // sort alphabetically
  25.         AcGePoint3d a = pairOne.second, b = pairTwo.second;
  26.         if (a.x == b.x)
  27.         {
  28.                 if (a.y == b.y)
  29.                         return (a.z > b.z);
  30.                 else
  31.                         return (a.y > b.y);
  32.         }
  33.         else
  34.                 return (a.x > b.x);
  35. }
  36.  
  37. static void LESQWordPuzzle_WORDPUZZLE(void)
  38. {
  39.         AcArray<AcString> words;
  40.         words.append(_T("BOX"));
  41.         words.append(_T("MAD"));
  42.         words.append(_T("AMASS"));
  43.         words.append(_T("LSP"));
  44.         words.append(_T("EGG"));
  45.         words.append(_T("AXE"));
  46.         words.append(_T("ATTIC"));
  47.         words.append(_T("GLAD"));
  48.         ads_name ss;
  49.         resbuf *rbFilter = acutBuildList(RTDXF0, _T("TEXT"), RTNONE);
  50.         if (acedSSGet(NULL, NULL, NULL, rbFilter, ss) != RTNORM)
  51.         {
  52.                 acutRelRb(rbFilter); return;
  53.         }
  54.         acutRelRb(rbFilter);
  55.         long length = 0;
  56.         if ((acedSSLength(ss, &length) != RTNORM) || (length == 0))
  57.         {
  58.                 acedSSFree(ss); return;
  59.         }
  60.         AcArray<std::pair<AcString, AcGePoint3d>> allCharacters;
  61.         AcArray<std::pair<AcString, AcGePoint3d>> characters;
  62.         AcArray<AcString> justStrings;
  63.         AcGePoint3dArray pts;
  64.         AcDbExtents ext;
  65.         AcDbObjectId id; ads_name ename;
  66.         for (int i = 0; i < length; i++)
  67.         {
  68.                 if (acedSSName(ss, i, ename) == RTNORM)
  69.                 {
  70.                         if (acdbGetObjectId(id, ename) == Acad::eOk)
  71.                         {
  72.                                 AcDbObjectPointer<AcDbText> pText(id, AcDb::kForRead);
  73.                                 if (pText.openStatus() == Acad::eOk)
  74.                                 {
  75.                                         AcString s = pText->textString();
  76.                                         for (int j = 0; j < words.length(); j++)
  77.                                         {
  78.                                                 AcString word = words[j];
  79.                                                 if (word.find(s) != -1)
  80.                                                 {
  81.                                                         pts.append(pText->alignmentPoint());
  82.                                                         ext.addPoint(pText->alignmentPoint());
  83.                                                         allCharacters.append(make_pair(s, pText->alignmentPoint()));
  84.                                                 }
  85.                                         }
  86.                                 }
  87.                         }
  88.                 }
  89.         }
  90.         if (allCharacters.length() > 0)
  91.         {
  92.                 AcDbBlockTableRecordPointer pBTR(acdbCurDwg()->currentSpaceId(), AcDb::kForWrite);
  93.                 if (pBTR.openStatus() != Acad::eOk) return;
  94.                 AcGePoint3d p1 = ext.minPoint();
  95.                 AcGePoint3d p3 = ext.maxPoint();
  96.                 AcGePoint3d p2(p3.x, p1.y, p1.z);
  97.                 AcGePoint3d p4(p1.x, p3.y, p1.z);
  98.                 AcGePoint3dArray pts_hor_top, pts_ver_left, pts_hor_bottom;
  99.                 for (int i = 0; i < pts.length(); i++)
  100.                 {
  101.                         if (pts[i].y == p4.y)
  102.                         {
  103.                                 if (!pts_hor_top.contains(pts[i]))
  104.                                 {
  105.                                         pts_hor_top.append(pts[i]);
  106.                                 }
  107.                         }
  108.                         if (pts[i].x == p4.x)
  109.                         {
  110.                                 if (!pts_ver_left.contains(pts[i]))
  111.                                 {
  112.                                         pts_ver_left.append(pts[i]);
  113.                                 }
  114.                         }
  115.                         if (pts[i].y == p1.y)
  116.                         {
  117.                                 if (!pts_hor_bottom.contains(pts[i]))
  118.                                 {
  119.                                         pts_hor_bottom.append(pts[i]);
  120.                                 }
  121.                         }
  122.                 }
  123.                 std::sort(pts_hor_top.asArrayPtr(), pts_hor_top.asArrayPtr() + pts_hor_top.length(), comparePoints);
  124.                 std::sort(pts_ver_left.asArrayPtr(), pts_ver_left.asArrayPtr() + pts_ver_left.length(), comparePoints);
  125.                 std::sort(pts_hor_bottom.asArrayPtr(), pts_hor_bottom.asArrayPtr() + pts_hor_bottom.length(), comparePoints);
  126.                 int rows = pts_hor_top.length();
  127.                 int columns = rows;
  128.                 double d = p4.distanceTo(p3) / (rows - 1);
  129.                 AcArray<AcArray<std::pair<AcString, AcGePoint3d>>> horizontals, verticals, diagonals;
  130.                 AcArray<std::pair<AcString, AcGePoint3d>> horizontal, vertical, diagonal;
  131.                 // save horizontals up-down
  132.                 AcGePoint3d pt = p4;
  133.                 for (int row = 0; row < rows; row++)
  134.                 {
  135.                         for (int i = 0; i < allCharacters.length(); i++)
  136.                         {
  137.                                 AcGePoint3d ptSearch = allCharacters[i].second;
  138.                                 if (pt.y == ptSearch.y)
  139.                                 {
  140.                                         if (!horizontal.contains(allCharacters[i]) && (horizontal.length() < rows))
  141.                                         {
  142.                                                 horizontal.append(allCharacters[i]);
  143.                                         }
  144.                                 }
  145.                         }
  146.                         if (!horizontals.contains(horizontal))
  147.                         {
  148.                                 std::sort(horizontal.asArrayPtr(), horizontal.asArrayPtr() + horizontal.length(), compare);
  149.                                 horizontal.reverse();
  150.                                 horizontals.append(horizontal);
  151.                         }
  152.                         horizontal.removeAll();
  153.                         pt.set(pt.x, pt.y - d, pt.z); // move down
  154.                 }
  155.                 // save verticals up-down
  156.                 pt = p4;
  157.                 for (int row = 0; row < rows; row++)
  158.                 {
  159.                         for (int i = 0; i < allCharacters.length(); i++)
  160.                         {
  161.                                 AcGePoint3d ptSearch = allCharacters[i].second;
  162.                                 if (pt.x == ptSearch.x)
  163.                                 {
  164.                                         if (!vertical.contains(allCharacters[i]) && (vertical.length() < rows))
  165.                                         {
  166.                                                 vertical.append(allCharacters[i]);
  167.                                         }
  168.                                 }
  169.                         }
  170.                         if (!verticals.contains(vertical))
  171.                         {
  172.                                 std::sort(vertical.asArrayPtr(), vertical.asArrayPtr() + vertical.length(), compare);
  173.                                 verticals.append(vertical);
  174.                         }
  175.                         vertical.removeAll();
  176.                         pt.set(pt.x + d, pt.y - d, pt.z);
  177.                 }
  178.                 AcGeVector3d v1 = (p2 - p4).normalize();
  179.                 AcGeVector3d v2 = (p3 - p1).normalize();
  180.                 AcGePoint3d ptx(p4.x - d, p4.y - d, p4.z);
  181.                 double ddiag = p4.distanceTo(ptx);
  182.                 // save diagonals
  183.                 pts_hor_top.reverse();
  184.                 for (int j = 0; j < pts_hor_top.length(); j++)
  185.                 {
  186.                         pt = pts_hor_top[j];
  187.                         for (int row = 0; row < rows; row++)
  188.                         {
  189.                                 for (int i = 0; i < allCharacters.length(); i++)
  190.                                 {
  191.                                         AcGePoint3d ptSearch = allCharacters[i].second;
  192.                                         if (pt.isEqualTo(ptSearch))
  193.                                         {
  194.                                                 if (!diagonal.contains(allCharacters[i]))
  195.                                                 {
  196.                                                         diagonal.append(allCharacters[i]);
  197.                                                 }
  198.                                                 break;
  199.                                         }
  200.                                 }
  201.                                 pt = pt + (v1 * ddiag);
  202.                         }
  203.                         if (!diagonals.contains(diagonal))
  204.                         {
  205.                                 diagonals.append(diagonal);
  206.                         }
  207.                         diagonal.removeAll();
  208.                 }
  209.                 pts_ver_left.removeAt(0); // remove first spot
  210.                 for (int j = 0; j < pts_ver_left.length(); j++)
  211.                 {
  212.                         pt = pts_ver_left[j];
  213.                         diagonal.removeAll();
  214.                         for (int row = 0; row < rows; row++)
  215.                         {
  216.                                 for (int i = 0; i < allCharacters.length(); i++)
  217.                                 {
  218.                                         AcGePoint3d ptSearch = allCharacters[i].second;
  219.                                         if (pt.isEqualTo(ptSearch))
  220.                                         {
  221.                                                 if (!diagonal.contains(allCharacters[i]))
  222.                                                 {
  223.                                                         diagonal.append(allCharacters[i]);
  224.                                                 }
  225.                                                 break;
  226.                                         }
  227.                                 }
  228.                                 pt = pt + (v1 * ddiag);
  229.                         }
  230.                         if (!diagonals.contains(diagonal))
  231.                         {
  232.                                 diagonals.append(diagonal);
  233.                         }
  234.                         diagonal.removeAll();
  235.                 }
  236.                 // save diagonals
  237.                 pts_hor_bottom.reverse();
  238.                 for (int j = 0; j < pts_hor_bottom.length(); j++)
  239.                 {
  240.                         pt = pts_hor_bottom[j];
  241.                         diagonal.removeAll();
  242.                         for (int row = 0; row < rows; row++)
  243.                         {
  244.                                 for (int i = 0; i < allCharacters.length(); i++)
  245.                                 {
  246.                                         AcGePoint3d ptSearch = allCharacters[i].second;
  247.                                         if (pt.isEqualTo(ptSearch))
  248.                                         {
  249.                                                 if (!diagonal.contains(allCharacters[i]))
  250.                                                 {
  251.                                                         diagonal.append(allCharacters[i]);
  252.                                                 }
  253.                                                 break;
  254.                                         }
  255.                                 }
  256.                                 pt = pt + (v2 * ddiag);
  257.                         }
  258.                         if (!diagonals.contains(diagonal))
  259.                         {
  260.                                 diagonals.append(diagonal);
  261.                         }
  262.                         diagonal.removeAll();
  263.                 }
  264.                 for (int j = 0; j < pts_ver_left.length(); j++)
  265.                 {
  266.                         pt = pts_ver_left[j];
  267.                         diagonal.removeAll();
  268.                         for (int row = 0; row < rows; row++)
  269.                         {
  270.                                 for (int i = 0; i < allCharacters.length(); i++)
  271.                                 {
  272.                                         AcGePoint3d ptSearch = allCharacters[i].second;
  273.                                         if (pt.isEqualTo(ptSearch))
  274.                                         {
  275.                                                 if (!diagonal.contains(allCharacters[i]))
  276.                                                 {
  277.                                                         diagonal.append(allCharacters[i]);
  278.                                                 }
  279.                                                 break;
  280.                                         }
  281.                                 }
  282.                                 pt = pt + (v2 * ddiag);
  283.                         }
  284.                         if (!diagonals.contains(diagonal))
  285.                         {
  286.                                 diagonals.append(diagonal);
  287.                         }
  288.                         diagonal.removeAll();
  289.                 }
  290.                 // look for the word
  291.                 for (int k = 0; k < words.length(); k++)
  292.                 {
  293.                         AcString word = words[k];
  294.                         AcArray<AcString> temp;
  295.                         AcGePoint3d sp, ep;
  296.                         bool found = false;
  297.                         AcString firstChar = word.substr(0, 1);
  298.                         AcString lastChar = word.substr(word.length() - 1, 1);
  299.                         // search on horizontals
  300.                         for (int i = 0; i < horizontals.length(); i++)
  301.                         {
  302.                                 AcArray<std::pair<AcString, AcGePoint3d>> row_horizontal = horizontals[i];
  303.                                 temp.removeAll();
  304.                                 for (int j = 0; j < row_horizontal.length(); j++)
  305.                                 {
  306.                                         AcString s = row_horizontal[j].first;
  307.                                         if (word.find(s) != -1)
  308.                                         {
  309.                                                 temp.append(s);
  310.                                         }
  311.                                 }
  312.                                 int counter = 0;
  313.                                 bool hasDupplicates = false;
  314.                                 bool atTheEnd = false;
  315.                                 bool startLeftRight = false;
  316.                                 AcArray<AcString> chars;
  317.                                 for (int c = 0; c < word.length(); c++)
  318.                                 {
  319.                                         AcString ch = word.substr(c, 1);
  320.                                         chars.append(ch);
  321.                                         if (temp.contains(ch))
  322.                                         {
  323.                                                 counter++;
  324.                                         }
  325.                                 }
  326.                                 for (int i = 0; i < chars.length(); i++)
  327.                                 {
  328.                                         AcString s = chars[i];
  329.                                         for (int ii = 0; ii < chars.length(); ii++)
  330.                                         {
  331.                                                 if (chars[ii] == s)
  332.                                                 {
  333.                                                         if (chars[ii+1] == s)
  334.                                                         {
  335.                                                                 hasDupplicates = true;
  336.                                                                 if (ii+1 == (chars.length() - 1))
  337.                                                                 {
  338.                                                                         atTheEnd = true;
  339.                                                                 }
  340.                                                                 break;
  341.                                                         }
  342.                                                 }
  343.                                         }
  344.                                 }
  345.                                 if (temp.length() >= word.length() && counter >= word.length())
  346.                                 {
  347.                                         if (temp[0] == firstChar && temp[1] == word.substr(1, 1))
  348.                                         {
  349.                                                 startLeftRight = true;
  350.                                         }
  351.                                         else
  352.                                         {
  353.                                                 startLeftRight = false;
  354.                                         }
  355.                                         bool foundSP = false;
  356.                                         bool foundEP = false;
  357.                                         for (int i = 0; i < row_horizontal.length(); i++)
  358.                                         {
  359.                                                 if (row_horizontal[i].first == firstChar)
  360.                                                 {
  361.                                                         sp = row_horizontal[i].second;
  362.                                                         foundSP = true;
  363.                                                         break;
  364.                                                 }
  365.                                         }
  366.                                         for (int i = 0; i < row_horizontal.length(); i++)
  367.                                         {
  368.                                                 if (row_horizontal[i].first == lastChar)
  369.                                                 {
  370.                                                         ep = row_horizontal[i].second;
  371.                                                         if (atTheEnd)
  372.                                                         {
  373.                                                                 ep = row_horizontal[i+1].second;
  374.                                                         }
  375.                                                         foundEP = true;
  376.                                                         break;
  377.                                                 }
  378.                                         }
  379.                                         if (foundSP && foundEP && startLeftRight)
  380.                                         {
  381.                                                 AcDbObjectPointer<AcDbLine> pLine;
  382.                                                 pLine.create();
  383.                                                 pLine->setStartPoint(sp);
  384.                                                 pLine->setEndPoint(ep);
  385.                                                 pBTR->appendAcDbEntity(pLine);
  386.                                         }
  387.                                         else
  388.                                         {
  389.                                                 row_horizontal.reverse();
  390.                                                 for (int i = 0; i < row_horizontal.length(); i++)
  391.                                                 {
  392.                                                         if (row_horizontal[i].first == firstChar)
  393.                                                         {
  394.                                                                 sp = row_horizontal[i].second;
  395.                                                                 break;
  396.                                                         }
  397.                                                 }
  398.                                                 for (int i = 0; i < row_horizontal.length(); i++)
  399.                                                 {
  400.                                                         if (row_horizontal[i].first == lastChar)
  401.                                                         {
  402.                                                                 ep = row_horizontal[i].second;
  403.                                                                 if (atTheEnd)
  404.                                                                 {
  405.                                                                         ep = row_horizontal[i+1].second;
  406.                                                                 }
  407.                                                                 break;
  408.                                                         }
  409.                                                 }
  410.                                                 AcDbObjectPointer<AcDbLine> pLine;
  411.                                                 pLine.create();
  412.                                                 pLine->setStartPoint(sp);
  413.                                                 pLine->setEndPoint(ep);
  414.                                                 pBTR->appendAcDbEntity(pLine);
  415.                                         }
  416.                                         break;
  417.                                 }
  418.                         }
  419.                         // search on verticals
  420.                         for (int i = 0; i < verticals.length(); i++)
  421.                         {
  422.                                 AcArray<std::pair<AcString, AcGePoint3d>> row_vertical = verticals[i];
  423.                                 temp.removeAll();
  424.                                 for (int j = 0; j < row_vertical.length(); j++)
  425.                                 {
  426.                                         AcString s = row_vertical[j].first;
  427.                                         if (word.find(s) != -1)
  428.                                         {
  429.                                                 temp.append(s);
  430.                                         }
  431.                                 }
  432.                                 int counter = 0;
  433.                                 bool hasDupplicates = false;
  434.                                 bool atTheEnd = false;
  435.                                 bool startLeftRight = false;
  436.                                 AcArray<AcString> chars;
  437.                                 for (int c = 0; c < word.length(); c++)
  438.                                 {
  439.                                         AcString ch = word.substr(c, 1);
  440.                                         chars.append(ch);
  441.                                         if (temp.contains(ch))
  442.                                         {
  443.                                                 counter++;
  444.                                         }
  445.                                 }
  446.                                 for (int i = 0; i < chars.length(); i++)
  447.                                 {
  448.                                         AcString s = chars[i];
  449.                                         for (int ii = 0; ii < chars.length(); ii++)
  450.                                         {
  451.                                                 if (chars[ii] == s)
  452.                                                 {
  453.                                                         if (chars[ii+1] == s)
  454.                                                         {
  455.                                                                 hasDupplicates = true;
  456.                                                                 if (ii+1 == (chars.length() - 1))
  457.                                                                 {
  458.                                                                         atTheEnd = true;
  459.                                                                 }
  460.                                                                 break;
  461.                                                         }
  462.                                                 }
  463.                                         }
  464.                                 }
  465.                                 if (temp.length() >= word.length() && counter >= word.length())
  466.                                 {
  467.                                         if (temp[0] == firstChar && temp[1] == word.substr(1, 1))
  468.                                         {
  469.                                                 startLeftRight = true;
  470.                                         }
  471.                                         else
  472.                                         {
  473.                                                 startLeftRight = false;
  474.                                         }
  475.                                         bool foundSP = false;
  476.                                         bool foundEP = false;
  477.                                         for (int i = 0; i < row_vertical.length(); i++)
  478.                                         {
  479.                                                 if (row_vertical[i].first == firstChar)
  480.                                                 {
  481.                                                         sp = row_vertical[i].second;
  482.                                                         foundSP = true;
  483.                                                         break;
  484.                                                 }
  485.                                         }
  486.                                         for (int i = 0; i < row_vertical.length(); i++)
  487.                                         {
  488.                                                 if (row_vertical[i].first == lastChar)
  489.                                                 {
  490.                                                         ep = row_vertical[i].second;
  491.                                                         if (atTheEnd)
  492.                                                         {
  493.                                                                 ep = row_vertical[i+1].second;
  494.                                                         }
  495.                                                         foundEP = true;
  496.                                                         break;
  497.                                                 }
  498.                                         }
  499.                                         if (foundSP && foundEP && startLeftRight)
  500.                                         {
  501.                                                 AcDbObjectPointer<AcDbLine> pLine;
  502.                                                 pLine.create();
  503.                                                 pLine->setStartPoint(sp);
  504.                                                 pLine->setEndPoint(ep);
  505.                                                 pBTR->appendAcDbEntity(pLine);
  506.                                         }
  507.                                         else
  508.                                         {
  509.                                                 row_vertical.reverse();
  510.                                                 for (int i = 0; i < row_vertical.length(); i++)
  511.                                                 {
  512.                                                         if (row_vertical[i].first == firstChar)
  513.                                                         {
  514.                                                                 sp = row_vertical[i].second;
  515.                                                                 break;
  516.                                                         }
  517.                                                 }
  518.                                                 for (int i = 0; i < row_vertical.length(); i++)
  519.                                                 {
  520.                                                         if (row_vertical[i].first == lastChar)
  521.                                                         {
  522.                                                                 ep = row_vertical[i].second;
  523.                                                                 if (atTheEnd)
  524.                                                                 {
  525.                                                                         ep = row_vertical[i+1].second;
  526.                                                                 }
  527.                                                                 break;
  528.                                                         }
  529.                                                 }
  530.                                                 AcDbObjectPointer<AcDbLine> pLine;
  531.                                                 pLine.create();
  532.                                                 pLine->setStartPoint(sp);
  533.                                                 pLine->setEndPoint(ep);
  534.                                                 pBTR->appendAcDbEntity(pLine);
  535.                                         }
  536.                                 }
  537.                                 temp.removeAll();
  538.                         }
  539.                         // search on diagonals
  540.                         for (int i = 0; i < diagonals.length(); i++)
  541.                         {
  542.                                 AcArray<std::pair<AcString, AcGePoint3d>> row_diagonal = diagonals[i];
  543.                                 temp.removeAll();
  544.                                 for (int j = 0; j < row_diagonal.length(); j++)
  545.                                 {
  546.                                         AcString s = row_diagonal[j].first;
  547.                                         if (word.find(s) != -1)
  548.                                         {
  549.                                                 temp.append(s);
  550.                                         }
  551.                                 }
  552.                                 int counter = 0;
  553.                                 bool hasDupplicates = false;
  554.                                 bool atTheEnd = false;
  555.                                 bool startLeftRight = false;
  556.                                 AcArray<AcString> chars;
  557.                                 for (int c = 0; c < word.length(); c++)
  558.                                 {
  559.                                         AcString ch = word.substr(c, 1);
  560.                                         chars.append(ch);
  561.                                         if (temp.contains(ch))
  562.                                         {
  563.                                                 counter++;
  564.                                         }
  565.                                 }
  566.                                 for (int i = 0; i < chars.length(); i++)
  567.                                 {
  568.                                         AcString s = chars[i];
  569.                                         for (int ii = 0; ii < chars.length(); ii++)
  570.                                         {
  571.                                                 if (chars[ii] == s)
  572.                                                 {
  573.                                                         if (chars[ii+1] == s)
  574.                                                         {
  575.                                                                 hasDupplicates = true;
  576.                                                                 if (ii+1 == (chars.length() - 1))
  577.                                                                 {
  578.                                                                         atTheEnd = true;
  579.                                                                 }
  580.                                                                 break;
  581.                                                         }
  582.                                                 }
  583.                                         }
  584.                                 }
  585.                                 if (temp.length() >= word.length() && counter >= word.length())
  586.                                 {
  587.                                         if (temp[0] == firstChar && temp[1] == word.substr(1, 1))
  588.                                         {
  589.                                                 startLeftRight = true;
  590.                                         }
  591.                                         else
  592.                                         {
  593.                                                 startLeftRight = false;
  594.                                         }
  595.                                         bool foundSP = false;
  596.                                         bool foundEP = false;
  597.                                         for (int i = 0; i < row_diagonal.length(); i++)
  598.                                         {
  599.                                                 if (row_diagonal[i].first == firstChar)
  600.                                                 {
  601.                                                         sp = row_diagonal[i].second;
  602.                                                         foundSP = true;
  603.                                                         break;
  604.                                                 }
  605.                                         }
  606.                                         for (int i = 0; i < row_diagonal.length(); i++)
  607.                                         {
  608.                                                 if (row_diagonal[i].first == lastChar)
  609.                                                 {
  610.                                                         ep = row_diagonal[i].second;
  611.                                                         if (atTheEnd)
  612.                                                         {
  613.                                                                 ep = row_diagonal[i+1].second;
  614.                                                         }
  615.                                                         foundEP = true;
  616.                                                         break;
  617.                                                 }
  618.                                         }
  619.                                         if (foundSP && foundEP && startLeftRight)
  620.                                         {
  621.                                                 AcDbObjectPointer<AcDbLine> pLine;
  622.                                                 pLine.create();
  623.                                                 pLine->setStartPoint(sp);
  624.                                                 pLine->setEndPoint(ep);
  625.                                                 pBTR->appendAcDbEntity(pLine);
  626.                                         }
  627.                                         else
  628.                                         {
  629.                                                 row_diagonal.reverse();
  630.                                                 for (int i = 0; i < row_diagonal.length(); i++)
  631.                                                 {
  632.                                                         if (row_diagonal[i].first == firstChar)
  633.                                                         {
  634.                                                                 sp = row_diagonal[i].second;
  635.                                                                 break;
  636.                                                         }
  637.                                                 }
  638.                                                 for (int i = 0; i < row_diagonal.length(); i++)
  639.                                                 {
  640.                                                         if (row_diagonal[i].first == lastChar)
  641.                                                         {
  642.                                                                 ep = row_diagonal[i].second;
  643.                                                                 if (atTheEnd)
  644.                                                                 {
  645.                                                                         ep = row_diagonal[i+1].second;
  646.                                                                 }
  647.                                                                 break;
  648.                                                         }
  649.                                                 }
  650.                                                 AcDbObjectPointer<AcDbLine> pLine;
  651.                                                 pLine.create();
  652.                                                 pLine->setStartPoint(sp);
  653.                                                 pLine->setEndPoint(ep);
  654.                                                 pBTR->appendAcDbEntity(pLine);
  655.                                         }
  656.                                 }
  657.                                 temp.removeAll();
  658.                         }
  659.                 }
  660.         }
  661. }
  662.  
[/code]

and if someone wants to see if works > did my tests on autocad 2011 < command: WordPuzzle

note: the code can be refactored - but did not have any time for that.
le.-
« Last Edit: May 07, 2012, 09:13:28 PM by LE »