summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/force/jedi/initialpositioning/GridInitialPositioning.lua
blob: cb710b35d60846d49c5b9e74bcf9c327328d673d (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 grid with square cells with width |node distance|
local declare = require "pgf.gd.interface.InterfaceToAlgorithms".declare
local InitialTemplate = require "pgf.gd.force.jedi.base.InitialTemplate"
local lib = require "pgf.gd.lib"

local GridInitialPositioning =  lib.class { base_class = InitialTemplate }


---
declare {
  key = "grid initial position",
  algorithm = GridInitialPositioning,
  phase = "initial positioning force framework",
}

-- Implementation starts here:

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

function GridInitialPositioning:run()
  -- locals for speed
  local vertices = self.vertices
  local dist = self.options["node distance"]
  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 n = math.ceil(math.sqrt(#vertices))
  local x = -dist
  local y = 0

  for i, vertex in ipairs(vertices) do
    -- place all other vertices with respect to the one already placed
    if placed[vertex] == nil then
      if i <= (y/dist+1)*n then
        x = x + dist
      else 
        x = 0
        y = y + dist
      end
      local p = vertex.pos
      p.x = x + centroid_x
      p.y = y + centroid_y
    end
  end
end


return GridInitialPositioning