summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/force/jedi/initialpositioning/RandomInitialPositioning.lua
blob: ce3a40582c56d4062eef0a0960799ef867af10ab (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
-- 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 initial position algorithm for graph drawing,
-- placing the vertices at random positions.
local declare = require "pgf.gd.interface.InterfaceToAlgorithms".declare
local InitialTemplate = require "pgf.gd.force.jedi.base.InitialTemplate"
local lib = require "pgf.gd.lib"

local RandomInitialPositioning = lib.class { base_class = InitialTemplate }

---
declare {
  key = "random initial position",
  algorithm = RandomInitialPositioning,
  phase = "initial positioning force framework"
}

-- Implementation starts here:

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

function RandomInitialPositioning:run()
  -- locals for speed
  local random = lib.random
  local vertices = self.vertices
  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)

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

return RandomInitialPositioning