I tried coloring objects, following an equal pattern to electrical field (responding to the placement of a specific type of objects called "charge"). Color is a way of representing the intensity of the electric field: whenever the electric field is positive there will be a shade of red, if it is negative, blue, and if the field is 0 it will be black (objects are representations of points in space, they are a way of "discretizing" space, I placed them inside a matrix).
The goal of this is to make a representation of the electric field similar to the representation of the potential of this PhET simulation.
Based on the principle of super position, my attempt was to define a function that first does a calculation of the distance of the charge towards each object, then I tried to color each object by adding values in the red channel (I am assuming that the placed charge is positive), according to its position, as long as its values in this channel are within a limit or the object is completely black; in case it has values in the blue channel, it will subtract them. And vice versa for blue.
def ncolor(r,g,b):
nr = int(r)
ng = int(g)
nb = int(b)
color = "#%4.4x%4.4x%4.4x" % (nr,ng,nb)
return color
def interaction(charge, pol):
for o in range(columnobjects):
for p in range(rowobjects):
(r, g, b) = window.winfo_rgb(canvas.itemcget(objectop, "fill"))
dis = math.sqrt((charge.x - objectoj.x)**2 + (charge.y - objectoj.y)**2)
if pol == 0:
if dis <= 100:
if float(r)==0 and float(b)==0 or 0 < float(r) < 10536:
(r,g,b) = np.array((r,g,b))+np.array((5268*2, 0, 0))
color = ncolor(r,g,b)
canvas.itemconfig(objectop, fill=color)
elif float(r)==0 and float(b)!=0:
(r,g,b) = np.array((r,g,b))-np.array((0, 0, 5268*2))
color = ncolor(r,g,b)
canvas.itemconfig(objectop, fill=color)
elif 100 < dis <= 200:
if float(r)==0 and float(b)==0 or 0 < float(r) < 52680:
(r,g,b) = np.array((r,g,b))+np.array((5268, 0, 0))
color = ncolor(r,g,b)
canvas.itemconfig(objectop, fill=color)
elif float(r)==0 and float(b)!=0:
(r,g,b) = np.array((r,g,b))-np.array((0, 0, 5268))
color = ncolor(r,g,b)
canvas.itemconfig(objectop, fill=color)
else: pass
if pol == 1:
if dis <= 100:
if float(r)==0 and float(b)==0 or 0 < float(r) < 10536:
(r,g,b) = np.array((r,g,b))+np.array((0, 0, 5268*2))
color = ncolor(r,g,b)
canvas.itemconfig(objectop, fill=color)
elif float(r)==0 and float(b)!=0:
(r,g,b) = np.array((r,g,b))-np.array((5268*2, 0, 0))
color = ncolor(r,g,b)
canvas.itemconfig(objectop, fill=color)
elif 100 < dis <= 200:
if float(r)==0 and float(b)==0 or 0 < float(r) < 52680:
(r,g,b) = np.array((r,g,b))+np.array((0, 0, 5268))
color = ncolor(r,g,b)
canvas.itemconfig(objectop, fill=color)
elif float(r)==0 and float(b)!=0:
(r,g,b) = np.array((r,g,b))-np.array((5268, 0, 0))
color = ncolor(r,g,b)
canvas.itemconfig(objectop, fill=color)
else: pass
When I ran it I realized that this is nothing like the behavior of an electric field produced by n charges. It occurs to me that, perhaps, I could directly apply the definition of electric field to the distance, and use the result in the rgb channels, but I don't know how to deal with negative values (I am new to working with rgb).