kopia lustrzana https://github.com/villares/sketch-a-day
main
rodzic
226738d8c5
commit
1a3c96fda3
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 1.7 MiB |
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 359 KiB |
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 227 KiB |
|
@ -0,0 +1,40 @@
|
|||
x = -400
|
||||
y = -400
|
||||
xv, yv = -1, -1
|
||||
|
||||
def setup():
|
||||
size(600, 400)
|
||||
global img
|
||||
img = loadImage("P1030601.JPG")
|
||||
# img = loadImage("P1030634.JPG")
|
||||
frameRate(30)
|
||||
noSmooth()
|
||||
|
||||
def draw():
|
||||
global x, y, xv, yv
|
||||
background(0)
|
||||
step = 4
|
||||
strokeWeight(3)
|
||||
for i in range(0, width, step):
|
||||
for j in range(0, height, step):
|
||||
if inside_img(i - x, j - y, img):
|
||||
c = img.get(i - x, j - y)
|
||||
stroke(c)
|
||||
point(i,j)
|
||||
# image(img, x, y)
|
||||
x = int(x + xv)
|
||||
y = int(y + yv)
|
||||
if x < -img.width + width: xv = -xv
|
||||
if x > 0: xv = -xv
|
||||
if y < -img.height + height: yv = -yv
|
||||
if y > 0: yv = -yv
|
||||
print(frameRate, x, y)
|
||||
|
||||
def inside_img(x, y, img):
|
||||
return 0 < x < img.width and 0 < y < img.height
|
||||
|
||||
def keyPressed():
|
||||
global x, y, xv, xy
|
||||
if key == ' ':
|
||||
xv = random(-3, 3)
|
||||
yv = random(-3, 3)
|
|
@ -0,0 +1,65 @@
|
|||
pa = [(100, 100), (300, 50), (300, 300)]
|
||||
pb = [(150, 150), (250, 100), (250, 250), (100, 250)]
|
||||
|
||||
|
||||
def setup():
|
||||
size(400, 400)
|
||||
strokeWeight(5)
|
||||
|
||||
def draw():
|
||||
|
||||
pc = clip_poly(pa, pb)
|
||||
|
||||
noFill()
|
||||
stroke(200, 0, 0)
|
||||
draw_poly(pa)
|
||||
stroke(0, 200, 0)
|
||||
draw_poly(pb)
|
||||
if keyPressed:
|
||||
stroke(0, 0, 200)
|
||||
draw_poly(pc)
|
||||
|
||||
|
||||
|
||||
def draw_poly(poly, closed=True):
|
||||
beginShape()
|
||||
for p in poly:
|
||||
vertex(*p)
|
||||
if closed:
|
||||
endShape(CLOSE)
|
||||
else:
|
||||
endShape()
|
||||
|
||||
|
||||
def clip_poly(subjectPolygon, clipPolygon):
|
||||
def inside(p):
|
||||
return(cp2[0]-cp1[0])*(p[1]-cp1[1]) > (cp2[1]-cp1[1])*(p[0]-cp1[0])
|
||||
|
||||
def computeIntersection():
|
||||
dc = [ cp1[0] - cp2[0], cp1[1] - cp2[1] ]
|
||||
dp = [ s[0] - e[0], s[1] - e[1] ]
|
||||
n1 = cp1[0] * cp2[1] - cp1[1] * cp2[0]
|
||||
n2 = s[0] * e[1] - s[1] * e[0]
|
||||
n3 = 1.0 / (dc[0] * dp[1] - dc[1] * dp[0])
|
||||
return [(n1*dp[0] - n2*dc[0]) * n3, (n1*dp[1] - n2*dc[1]) * n3]
|
||||
|
||||
outputList = subjectPolygon
|
||||
cp1 = clipPolygon[-1]
|
||||
|
||||
for clipVertex in clipPolygon:
|
||||
cp2 = clipVertex
|
||||
inputList = outputList
|
||||
outputList = []
|
||||
s = inputList[-1]
|
||||
|
||||
for subjectVertex in inputList:
|
||||
e = subjectVertex
|
||||
if inside(e):
|
||||
if not inside(s):
|
||||
outputList.append(computeIntersection())
|
||||
outputList.append(e)
|
||||
elif inside(s):
|
||||
outputList.append(computeIntersection())
|
||||
s = e
|
||||
cp1 = cp2
|
||||
return(outputList)
|
|
@ -0,0 +1,69 @@
|
|||
pa = [(100, 100), (300, 50), (300, 300)]
|
||||
pb = [(150, 150), (250, 100), (250, 250), (100, 250)]
|
||||
|
||||
|
||||
def setup():
|
||||
size(400, 400)
|
||||
strokeWeight(5)
|
||||
|
||||
def draw():
|
||||
|
||||
pc = clip_poly(pa, pb)
|
||||
|
||||
noFill()
|
||||
stroke(200, 0, 0)
|
||||
draw_poly(pa)
|
||||
stroke(0, 200, 0)
|
||||
draw_poly(pb)
|
||||
if keyPressed:
|
||||
stroke(0, 0, 200)
|
||||
draw_poly(pc)
|
||||
|
||||
|
||||
|
||||
def draw_poly(poly, closed=True):
|
||||
beginShape()
|
||||
for p in poly:
|
||||
vertex(*p)
|
||||
if closed:
|
||||
endShape(CLOSE)
|
||||
else:
|
||||
endShape()
|
||||
|
||||
|
||||
def clip_poly(subjectPolygon, clipPolygon):
|
||||
|
||||
def inside(p):
|
||||
return (cp2[0] - cp1[0]) * (p[1] - cp1[1]) > (cp2[1] - cp1[1]) * (p[0] - cp1[0])
|
||||
|
||||
def intersection():
|
||||
dc = [cp1[0] - cp2[0], cp1[1] - cp2[1]],
|
||||
dp = [s[0] - e[0], s[1] - e[1]],
|
||||
n1 = cp1[0] * cp2[1] - cp1[1] * cp2[0],
|
||||
n2 = s[0] * e[1] - s[1] * e[0],
|
||||
n3 = 1.0 / (dc[0] * dp[1] - dc[1] * dp[0])
|
||||
return [(n1 * dp[0] - n2 * dc[0]) * n3, (n1 * dp[1] - n2 * dc[1]) * n3]
|
||||
|
||||
outputList = subjectPolygon
|
||||
cp1 = clipPolygon[-1]
|
||||
for j in clipPolygon:
|
||||
cp2 = clipPolygon[j]
|
||||
inputList = outputList
|
||||
outputList = []
|
||||
s = inputList[-1] # last on the input list
|
||||
for i in inputList:
|
||||
e = inputList[i]
|
||||
if (inside(e)):
|
||||
if not inside(s):
|
||||
outputList.append(intersection())
|
||||
|
||||
outputList.append(e)
|
||||
|
||||
elif (inside(s)):
|
||||
outputList.append(intersection())
|
||||
|
||||
s = e
|
||||
|
||||
cp1 = cp2
|
||||
|
||||
return outputList
|
Ładowanie…
Reference in New Issue