X-Git-Url: https://git.jsancho.org/?p=dungeon-master.git;a=blobdiff_plain;f=dungeon-master%2Fgeom%2Fvoronoi.scm;h=553beec920999f056c9526e25e7b7027579d2485;hp=9217285ad97c2295dd539acd6cba191284526801;hb=4fce641cc077d18f972e250d2fe3be5067618127;hpb=09c4aef3b2dde4e8fdf0a4b673b36c1ddbc84b6e diff --git a/dungeon-master/geom/voronoi.scm b/dungeon-master/geom/voronoi.scm index 9217285..553beec 100644 --- a/dungeon-master/geom/voronoi.scm +++ b/dungeon-master/geom/voronoi.scm @@ -1,3 +1,20 @@ +;;; Dungeon Master --- RPG Adventure Generator +;;; Copyright © 2019 Javier Sancho +;;; +;;; 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 . + + (define-module (dungeon-master geom voronoi) #:use-module (ice-9 receive) #:use-module (rnrs sorting) @@ -32,6 +49,14 @@ (> (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 @@ -42,7 +67,7 @@ (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) @@ -86,9 +111,27 @@ 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