Adjust color of voronoi depending on number of neighbors

This commit is contained in:
Ian Jauslin 2024-02-22 16:50:04 -05:00
parent 4a6d2a1758
commit a48457de29

View File

@ -176,39 +176,42 @@ class Painter(Widget):
while self.pos_tocoord_x(xx)<self.width: while self.pos_tocoord_x(xx)<self.width:
yy=pos.y yy=pos.y
while self.pos_tocoord_y(yy)<self.height: while self.pos_tocoord_y(yy)<self.height:
if self.is_in_voronoi(xx,yy,particle): self.draw_voronoi_site(xx,yy,particle.color,self.is_in_voronoi(xx,yy,particle))
self.draw_voronoi_site(xx,yy,particle.color)
yy+=self.lattice.spacing yy+=self.lattice.spacing
yy=pos.y-self.lattice.spacing yy=pos.y-self.lattice.spacing
while self.pos_tocoord_y(yy)>0: while self.pos_tocoord_y(yy)>0:
if self.is_in_voronoi(xx,yy,particle): self.draw_voronoi_site(xx,yy,particle.color,self.is_in_voronoi(xx,yy,particle))
self.draw_voronoi_site(xx,yy,particle.color)
yy-=self.lattice.spacing yy-=self.lattice.spacing
xx+=self.lattice.spacing xx+=self.lattice.spacing
xx=pos.x-self.lattice.spacing xx=pos.x-self.lattice.spacing
while self.pos_tocoord_x(xx)>0: while self.pos_tocoord_x(xx)>0:
yy=pos.y yy=pos.y
while self.pos_tocoord_y(yy)<self.height: while self.pos_tocoord_y(yy)<self.height:
if self.is_in_voronoi(xx,yy,particle): self.draw_voronoi_site(xx,yy,particle.color,self.is_in_voronoi(xx,yy,particle))
self.draw_voronoi_site(xx,yy,particle.color)
yy+=self.lattice.spacing yy+=self.lattice.spacing
yy=pos.y-self.lattice.spacing yy=pos.y-self.lattice.spacing
while self.pos_tocoord_y(yy)>0: while self.pos_tocoord_y(yy)>0:
if self.is_in_voronoi(xx,yy,particle): self.draw_voronoi_site(xx,yy,particle.color,self.is_in_voronoi(xx,yy,particle))
self.draw_voronoi_site(xx,yy,particle.color)
yy-=self.lattice.spacing yy-=self.lattice.spacing
xx-=self.lattice.spacing xx-=self.lattice.spacing
# check whether a site is in the Voronoi cell of a particle # check whether a site is in the Voronoi cell of a particle
def is_in_voronoi(self,x,y,particle): def is_in_voronoi(self,x,y,particle):
d_to_particle=self.lattice.distance_to_particle(x,y,particle) d_to_particle=self.lattice.distance_to_particle(x,y,particle)
# count how many are in voronoi cell
count=1
# TODO: start with a particle that is close to x,y # TODO: start with a particle that is close to x,y
for q in self.particles: for q in self.particles:
if q!=particle and self.lattice.distance_to_particle(x,y,q)<d_to_particle: dd=self.lattice.distance_to_particle(x,y,q)
return False if q!=particle and dd<d_to_particle:
return True return 0
if dd==d_to_particle:
count+=1
return count
# draw a site in a Voronoi cell # draw a site in a Voronoi cell
def draw_voronoi_site(self,x,y,color): def draw_voronoi_site(self,x,y,color,count):
Color(color[0],color[1],color[2],0.75) if count==0:
return
Color(color[0],color[1],color[2],1-count*0.1)
Rectangle(pos=(self.pos_tocoord_x(x-0.5),self.pos_tocoord_y(y-0.5)),size=(self.base_size,self.base_size)) Rectangle(pos=(self.pos_tocoord_x(x-0.5),self.pos_tocoord_y(y-0.5)),size=(self.base_size,self.base_size))