如何在类似lisp的语言中分解几何图形构建?

问题描述 投票:1回答:1

如何用类似lisp的语言分解这个过程?

  • 从一些几何图形开始。
  • 我们将它乘以并得到几个副本。
  • 然后我们将这个副本设置在其他图形的边框上,e.q。副本之间的距离相等。
  • 然后我们设置边界上每个对象的旋转,并依赖于边界上的位置。

在整个过程中分解整个过程有哪些方法:制作一个对象的副本,将其副本安排在另一个对象的边界上,设置其副本的旋转?

最后一步特别有趣,以及如何使用以前的步骤进行组合。

functional-programming lisp common-lisp decomposition
1个回答
7
投票

你的问题很广泛,让我们有一个2D形状的简单例子。

下面的代码定义为我们可以这样写:

(isomorphism (alexandria:compose (scale 10)
                                 (lift #'round)
                                 (rotate 90))
             (triangle (point 0 0)
                       (point 1 0)
                       (point 0 1)))

=> (TRIANGLE (POINT 0 0) (POINT 0 10) (POINT -10 0))

这计算了一个简单的变换函数,称为同构(保持形状),首先是旋转,然后是计算点的舍入,然后是缩放操作。结果是描述结果形状的列表。复制一个形状只是函数#'identity的同构(但如果你采用纯粹的函数方式它有点无用)。

注意:这里的舍入是例如当cos / sin给出非常小的浮子时,它会回落到零;浮子的使用打破了形状保留,并且圆形也是如此,但是当它们组合时,所得到的形状是实际的同构。根据您的需要,这一点的正确性/准确性可能会或可能不重要。您还可以描述应用了哪些转换,并仅“栅格化”它们以供显示。

变换函数在坐标列表上工作,并返回坐标列表:

(defun translate (dx &optional (dy dx))
  (lambda (xy) (mapcar #'+ xy (list dx dy))))

(defun scale (sx &optional (sy sx))
  (lambda (xy) (mapcar #'* xy (list sx sy))))

(defun rotate (degrees)
  (let* ((radians (* degrees pi 1/180))
         (cos (cos radians))
         (sin (sin radians)))
    (lambda (xy)
      (destructuring-bind (x y) xy
        (list (- (* x cos) (* y sin))
              (+ (* y cos) (* x sin)))))))

(defun lift (fn)
  (lambda (things)
    (mapcar fn things)))

isomorphism函数定义如下,并递归地将形状解构为类型标记(兼作构造函数)和组件,如果是点,则应用转换函数:

(defun isomorphism (transform shape)
  (flet ((isomorphism (s) (isomorphism transform s)))
    (with-shape (constructor components) shape
      (apply constructor
             (if (eq constructor 'point)
                 (funcall transform components)
                 (mapcar #'isomorphism components))))))

我将shapewith-shape定义如下,对它们的表示方式进行了一些抽象:

(defun shape (constructor components)
  (list* constructor components))

(defmacro with-shape ((constructor components) shape &body body)
  `(destructuring-bind (,constructor &rest ,components) ,shape
     ,@body))

我可以使用简单的函数定义形状,可能会也可能不会执行某些检查和规范化:

(defun point (&rest coords)
  (shape 'point coords))

(defun triangle (a b c)
  (shape 'triangle (list a b c)))

(defun rectangle (x0 y0 x1 y1)
  (shape 'rectangle
         (list (min x0 x1)
               (min y0 y1)
               (max x0 x1)
               (max y0 y1))))

请注意构造函数始终与函数名称的符号相同。这可以使用宏来强制执行,您只需要返回组件列表:

(defconstructor point (x y)
  (list x y))

您还可以从上面构建派生构造函数:

(defun rectangle-xywh (x y width height)
  (rectangle x y (+ x width) (+ y height)))

上面的形状是根据点定义的,但你可以想象从较小的形状组装形状:

(defun group (&rest shapes)
  (shape 'group shapes))

这是一个玩具示例,但这可能是一个有用的起点。

然后,如果你想拍摄一个形状并以90°的增量旋转不同的副本,你可以这样做:

(loop
  for angle from 0 below 360 by 90
  collect
  (isomorphism (compose (lift #'round)
                        (rotate angle)
                        (scale 2)
                        (translate 10 0))
               (group (triangle (point 0 0)
                                (point 1 0)
                                (point 0 1)))))
© www.soinside.com 2019 - 2024. All rights reserved.