(Vmon)
(Defun Set1 ()
  (Initget 1) (Setq P10 (Getpoint "\nEnter corner point: "))
  (Initget 1) (Setq P20 (Getcorner P10 "\nEnter diagonal corner point: ")
                    R3 (Abs (- (Car P10) (Car P20)))
                    R4 (Abs (- (Cadr P10) (Cadr P20)))
                    I1 (Fix (/ R3 R1))
                    I2 (Fix (/ R4 R2))
                    A1 (Angle P10 P20)
              )
  (Cond
    ((And (< A1 (* Pi 0.5)) (> A1 0))
      (Setq P1 P10
            P2 P20
      )
    )
    ((And (< A1 Pi) (> A1 (* Pi 0.5)))
      (Setq P1 (List (Car P20) (Cadr P10))
            P2 (List (Car P10) (Cadr P20))
      )
    )
    ((And (< A1 (* Pi 1.5)) (> A1 Pi))
      (Setq P1 P20
            P2 P10
      )
    )
    ((And (< A1 (* Pi 2)) (> A1 (* Pi 1.5)))
      (Setq P1 (List (Car P10) (Cadr P20))
            P2 (List (Car P20) (Cadr P10))
      )
    )
  )
)

(Defun C:Ce ()
  (Setvar "Cmdecho" 0)
  (Graphscr)
  (Initget 7) (Setq R1 (Getreal "\nEnter width of grid in inches: "))
  (Initget 7) (Setq R2 (Getreal "\nEnter height of grid in inches: "))
  (Initget 1 "CE CO")
  (Setq S1 (Substr (Getkword "\nCEnter or COrner justified: ") 1 2))
  (Cond
    ((= "CE" S1)
      (Initget 1 "G GT T")
      (Setq S2 (Getkword "Grid/Grid and Tile/Tile: "))
      (Cond
        ((= "G" S2)
          (Set1)
          (Setq P3 (Polar P1 (Angle P1 P2) (/ (Distance P1 P2) 2))
                P4 (List (Car P3) (Cadr P2))
                P5 (List (Car P3) (Cadr P1))
                I3 (Abs (1+ (Fix (/ (Distance P4 P2) R1))))
          )
          (Command "Line" P4 P5 ""
                   "Array" "L" "" "R" "1" I3 R1
                   "Array" "P" "" "R" "1" I3 (* -1 R1)
          )
          (Setq P4 (List (Car P1) (Cadr P3))
                P5 (List (Car P2) (Cadr P3))
                I3 (Abs (1+ (Fix (/ (Distance P4 P1) R2))))
          )
          (Command "Line" P4 P5 ""
                   "Array" "L" "" "R" I3 "1" R2
                   "Array" "P" "" "R" I3 "1" (* -1 R2)
          )
        )
        ((= "GT" S2)
          (Initget 1 "V H")
          (Setq S3 (Getkword "Grid to be Horizontal/Vertical: "))
            (Cond
              ((= "V" S3)
                (Set1)
                (Setq P3 (Polar P1 (Angle P1 P2) (/ (Distance P1 P2) 2))
                      P4 (List (Car P3) (Cadr P2))
                      P5 (List (Car P3) (Cadr P1))
                      I3 (Abs (1+ (Fix (/ (Distance P4 P2) R1))))
                )
                (Command "Line" P4 P5 ""
                         "Array" "L" "" "R" "1" I3 R1
                         "Array" "P" "" "R" "1" I3 (* -1 R1)
                )
                (Setq P4 (List (Car P1) (- (Cadr P3) (/ R2 2)))
                      I3 (Abs (1+ (Fix (/ (Distance P4 P1) R2))))
                      P5 (List (Car P2) (Cadr P4))
                )
                (Command "Line" P4 P5 ""
                         "Array" "L" "" "R" (1+ I3) "1" R2
                         "Array" "P" "" "R" I3 "1" (* -1 R2)
                )
              )
              ((= "H" S3)
                (Set1)
                (Setq P3 (Polar P1 (Angle P1 P2) (/ (Distance P1 P2) 2))
                      P4 (List (+ (Car P3) (/ R1 2)) (Cadr P2))
                      I3 (Abs (1+ (Fix (/ (Distance P4 P2) R1))))
                      P5 (List (Car P4) (Cadr P1))
                )
                (Command "Line" P4 P5 ""
                         "Array" "L" "" "R" "1" I3 R1
                         "Array" "P" "" "R" "1" (1+ I3) (* -1 R1)
                )
                (Setq P4 (List (Car P1) (Cadr P3))
                      P5 (List (Car P2) (Cadr P3))
                      I3 (Abs (1+ (Fix (/ (Distance P4 P1) R2))))
                )
                (Command "Line" P4 P5 ""
                         "Array" "L" "" "R" I3 "1" R2
                         "Array" "P" "" "R" I3 "1" (* -1 R2)
                )
              )
            )
        )
        ((= "T" S2)
          (Set1)
          (Setq P3 (Polar P1 (Angle P1 P2) (/ (Distance P1 P2) 2))
                P4 (List (+ (Car P3) (/ R1 2)) (Cadr P2))
                I3 (Abs (1+ (Fix (/ (Distance P4 P2) R1))))
                P5 (List (Car P4) (Cadr P1))
          )
          (Command "Line" P4 P5 ""
                   "Array" "L" "" "R" "1" I3 R1
                   "Array" "P" "" "R" "1" (1+ I3) (* -1 R1)
          )
          (Setq P4 (List (Car P1) (- (Cadr P3) (/ R2 2)))
                I3 (Abs (1+ (Fix (/ (Distance P4 P1) R2))))
                P5 (List (Car P2) (Cadr P4))
          )
          (Command "Line" P4 P5 ""
                   "Array" "L" "" "R" (1+ I3) "1" R2
                   "Array" "P" "" "R" I3 "1" (* -1 R2)
          )
        )
      )
    )
    ((= "CO" S1)
      (Set1)
      (If
        (< (Car P20) (Car P10))
          (Setq R1 (* -1 R1))
      )
      (If
        (< (Cadr P20) (Cadr P10))
          (Setq R2 (* -1 R2))
      )
      (Setq P3 (List (+ (Car P10) R1) (Cadr P10))
            P4 (List (Car P3) (Cadr P20))
      )
      (Command "Line" P3 P4 ""
               "Array" "L" "" "R" "1" I1 R1
      )
      (Setq P3 (List (Car P10) (+ (Cadr P10) R2))
            P4 (List (Car P20) (Cadr P3))
      )
      (Command "Line" P3 P4 ""
               "Array" "L" "" "R" I2 "1" R2
      )
    )
  )
  (Princ)
)
