+;;; Dungeon Master --- RPG Adventure Generator
+;;; Copyright © 2019 Javier Sancho <jsf@jsancho.org>
+;;;
+;;; Dungeon Master is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or
+;;; (at your option) any later version.
+;;;
+;;; Dungeon Master is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with Dungeon Master. If not, see <http://www.gnu.org/licenses/>.
+
+
(define-module (dungeon-master geom voronoi)
#:use-module (ice-9 receive)
#:use-module (rnrs sorting)
(> (angle-sign seed c1 c2) 0)))
(make-raw-voronoi-region seed (list-sort compare-angles vertices)))
+(define (voronoi-region-center region)
+ (let* ((vertices (voronoi-region-vertices region))
+ (centers (map (lambda (v) (triangle-center v)) vertices)))
+ (scale-point
+ (apply sum-points centers)
+ (/ 1 (length vertices)))))
+
+
;;; Voronoi mesh
(define-record-type <voronoi-mesh>
(frame voronoi-mesh-frame)
(regions voronoi-mesh-regions))
-(define* (make-voronoi-mesh vertices #:optional (relax-steps 3))
+(define (make-voronoi-mesh vertices)
;; Delaunay triangulation
(receive (triangles points frame)
(bowyer-watson vertices)
ordered-regions)))
filtered-regions))
-(define* (voronoi-mesh-relax voronoi #:optional (to-relax '()))
- (let ((regions (partitioning voronoi)))
- voronoi))
+(define* (voronoi-mesh-relax voronoi #:optional (points-to-relax '()))
+ (let ((regions
+ (map
+ (lambda (r) (cons (voronoi-region-seed r) (voronoi-region-center r)))
+ (partitioning voronoi)))
+ (points
+ (filter
+ (lambda (p) (not (member p (voronoi-mesh-frame voronoi))))
+ (voronoi-mesh-points voronoi)))
+ (to-relax
+ (if (null? points-to-relax)
+ (voronoi-mesh-points voronoi)
+ points-to-relax)))
+ (make-voronoi-mesh
+ (map
+ (lambda (point)
+ (let ((center (assoc-ref regions point)))
+ (if (and center (member point to-relax))
+ center
+ point)))
+ points))))
(define (make-regions points triangles)
(map-in-order