summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/force/jedi/initialpositioning/CircularInitialPositioning.lua
blob: 1ed35653c93e2d0a072756fa2a6922b61c477919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- Copyright 2014 by Ida Bruhns
--
-- This file may be distributed and/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information

--- This class implements an inital position algorithm for graph drawing, placing the vertices on  
-- a circle with th radius given by the |radius| key
local declare = require "pgf.gd.interface.InterfaceToAlgorithms".declare
local InitialTemplate = require "pgf.gd.force.jedi.base.InitialTemplate"
local lib = require "pgf.gd.lib"

local CircularInitialPositioning =  lib.class { base_class = InitialTemplate }


---
declare {
  key = "circular initial position",
  algorithm = CircularInitialPositioning,
  phase = "initial positioning force framework",
  phase_default = true 
}

-- Implementation starts here:

function CircularInitialPositioning:constructor ()
  InitialTemplate.constructor(self)
end

function CircularInitialPositioning:run()
  -- locals for speed
  local vertices = self.vertices
  local tmp =  (self.options["node pre sep"] + self.options["node post sep"]) + 
    (self.options["sibling pre sep"] + self.options["sibling post sep"])
  local min_radius = tmp * #self.vertices/2/math.pi
  local radius = math.max(self.options.radius, min_radius)
  local desired_vertices = self.desired_vertices
  -- place vertices where the |desired at | option has been set first
  local placed, centroid_x, centroid_y = InitialTemplate:desired(desired_vertices)
  local angle = 2*math.pi / #vertices
  local a = angle
  local sin = math.sin
  local cos = math.cos

  for _, vertex in ipairs(vertices) do
    -- place all other vertices with respect to the one already placed
    if placed[vertex] == nil then
      local p = vertex.pos
      p.x = sin(a) * radius + centroid_x
      p.y = cos(a) * radius + centroid_y
      a = a + angle
    end
  end
end


return CircularInitialPositioning