1# -*- coding: utf-8 -*-
2#  Copyright 2011 Takeshi KOMIYA
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16import math
17
18from blockdiag.utils import XY
19
20DIVISION = 1000.0
21CYCLE = 10
22
23
24def _angles(du, a, b, start, end):
25    phi = (start / 180.0) * math.pi
26    while phi <= (end / 180.0) * math.pi:
27        yield phi
28        phi += du / math.sqrt((a * math.sin(phi)) ** 2 +
29                              (b * math.cos(phi)) ** 2)
30
31
32def _coordinates(du, a, b, start, end):
33    for angle in _angles(du, a, b, start, end):
34        yield (a * math.cos(angle), b * math.sin(angle))
35
36
37def endpoints(du, a, b, start, end):
38    pt1 = next(iter(_coordinates(du, a, b, start, start + 1)))
39    pt2 = next(iter(_coordinates(du, a, b, end, end + 1)))
40
41    return [XY(*pt1), XY(*pt2)]
42
43
44def dots(box, cycle, start=0, end=360):
45    # calcrate rendering pattern from cycle
46    base = 0
47    rendered = []
48    for index in range(0, len(cycle), 2):
49        i, j = cycle[index:index + 2]
50        for n in range(base * 2, (base + i) * 2):
51            rendered.append(n)
52        base += i + j
53
54    a = float(box.width) / 2
55    b = float(box.height) / 2
56    du = 1
57    _max = sum(cycle) * 2
58    center = box.center
59    for i, coord in enumerate(_coordinates(du, a, b, start, end)):
60        if i % _max in rendered:
61            yield XY(center.x + coord[0], center.y + coord[1])
62