clone

a browser-first diagram editor with live svg previews, local drafts and encrypted, accountless sharing

main
diagramzip / renderers / python / blockdiag-family / src / blockdiag / imagedraw / utils / ellipse.py
ellipse.py 1.8K
1# -*- coding: utf-8 -*-2# Copyright 2011 Takeshi KOMIYA3#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 at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# 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 and14# limitations under the License.1516import math1718from blockdiag.utils import XY1920DIVISION = 1000.021CYCLE = 10222324def _angles(du, a, b, start, end):25 phi = (start / 180.0) * math.pi26 while phi <= (end / 180.0) * math.pi:27 yield phi28 phi += du / math.sqrt((a * math.sin(phi)) ** 2 +29 (b * math.cos(phi)) ** 2)303132def _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))353637def 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)))4041 return [XY(*pt1), XY(*pt2)]424344def dots(box, cycle, start=0, end=360):45 # calcrate rendering pattern from cycle46 base = 047 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 + j5354 a = float(box.width) / 255 b = float(box.height) / 256 du = 157 _max = sum(cycle) * 258 center = box.center59 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])