Last active
June 2, 2019 11:20
-
-
Save toshinoritakata/243397905335431c27913fd80b7902e3 to your computer and use it in GitHub Desktop.
http://members.chello.at/~easyfilter/bresenham.html のPython版 直線と円
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://members.chello.at/~easyfilter/bresenham.html | |
import math | |
size(420, 420) | |
noSmooth() | |
background(0) | |
stroke(255) | |
def plotLine(x0, y0, x1, y1): | |
dx = math.fabs(x1-x0) | |
dy = -math.fabs(y1-y0) | |
sx = 1 if x0 < x1 else -1 | |
sy = 1 if y0 < y1 else -1 | |
err = dx + dy | |
while True: | |
point(x0,y0) | |
if x0 == x1 and y0 == y1: | |
break | |
e2 = 2*err | |
if e2 >= dy: | |
err = err + dy | |
x0 = x0 + sx | |
if e2 <= dx: | |
err = err + dx | |
y0 = y0 + sy | |
def plotCircle(xm, ym, r): | |
x = -r | |
y = 0 | |
err = 2-2*r; # II. Quadrant | |
while True: | |
point(xm-x, ym+y); # I. Quadrant | |
point(xm-y, ym-x); # II. Quadrant | |
point(xm+x, ym-y); # III. Quadrant | |
point(xm+y, ym+x); # IV. Quadrant | |
r = err | |
if r <= y: | |
y += 1 | |
err += y*2+1 # e_xy+e_y < 0 | |
if r > x or err > y: | |
x += 1 | |
err += x*2+1; # e_xy+e_x > 0 or no 2nd y-step | |
if x >= 0: | |
break | |
for th in range(0, 360, 30): | |
x = math.cos(math.radians(th))*200 | |
y = math.sin(math.radians(th))*200 | |
plotLine(210, 210, int(210+x), int(210+y)) | |
for r in range(50, 201, 50): | |
plotCircle(210, 210, r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment