Skip to content

Instantly share code, notes, and snippets.

@blzzua
Last active November 28, 2019 14:24
Show Gist options
  • Save blzzua/063b1d8d05663c7c00fdf6fabc3a87b5 to your computer and use it in GitHub Desktop.
Save blzzua/063b1d8d05663c7c00fdf6fabc3a87b5 to your computer and use it in GitHub Desktop.
реализация эллипса на базе библиотеки graph
#!/usr/bin/python
# -*- coding: utf-8 -*-
from graph import *
from math import sin, cos, pi
def ellipse( x1, y1, x2, y2, vertex_count = 36 ):
"""
Рисует эллипс, вписанный в прямоугольник x1,y1 x x2,y2
"""
(x1, x2) = (min(x1, x2), max(x1, x2))
(y1, y2) = (min(y1, y2), max(y1, y2))
a = (x2 - x1) / 2
b = (y2 - y1) / 2
## https://ru.wikipedia.org/wiki/Эллипс#Уравнения_в_параметрической_форме
VERTEX = [ (a * cos(i * 2 * pi / vertex_count) + ( x1 + x2 ) / 2, \
b * sin(i * 2 * pi / vertex_count) + ( y1 + y2 ) / 2 ) \
for i in range(vertex_count) ]
mypol = polygon(VERTEX)
return mypol
if __name__ == '__main__':
"демонстрация. рисует разноцветные эллипсы"
from random import randrange
max_x = 500
max_y = 600
min_x = 10
min_y = 10
for i in range(20):
brushColor(randColor())
penColor(randColor())
(x1, y1) = (randrange(start=min_x, stop=max_x),
randrange(start=min_y, stop=max_y))
(x2, y2) = (randrange(start=min_x, stop=max_x),
randrange(start=min_y, stop=max_y))
ellipse(x1, y1, x2, y2)
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment