Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Points
#1
Hey everyone,
I just started coding python. I'm using jython and trying to create voronoi diagrams. For some reason I do not understand, my main loop does not work properly. The points are not placed on my gpanel. At this point I am really frustated, please point to the right direction.
(btw. I am a math teacher, so no homework here, just for me)

from gpanel import *
from math import *

nodeX=[]   #List of the x-coordinates of the nodes
nodeY=[]   #List of the y-coordinates of the nodes
nodeColor=[]   #List of the colors of the nodes


XX=60   #getScreenWidth() # reduced for testing
YY=60   #getScreenHeight()



def onMousePressed(xx,yy):
    nodeX.append(xx)
    nodeY.append(yy)
    nodeColor.append(getRandomX11Color())  #defines coordinats and colors of the nodes
    point(xx,yy)                    #put a point to mark the node
    if len(nodeX)>1:                   #jumps to MakeVoronoi
       MakeVoronoi()
    
def MakeVoronoi():
    for x in range(XX):             # Goes through all x-pixels
        for y in range(YY):         # Goes through all y-pixels
            dmin = math.sqrt(2)     # Distance set to maximum possilble
            l=-1                    # Number of the nearest node
            for r in range(len(nodeX)):# Cycles through nodes for each pixel
                dist=math.hypot(nodeX[r]-XX/(x+1),nodeY[r]-YY/(y+1)) #distance between the point (x/y) to node[r]
                if dist<dmin:       # if nodr(r) is nearer to the point, the point gets the color of the node
                    dmin=dist
                    l=r         
            setColor(nodeColor[l])  #Color for the point is set
            point(x+1,y+1)          #Point is painted (?)    
            
makeGPanel(Size(XX,YY),mousePressed=onMousePressed)
Reply
#2
I don't know the gpanel module, but from what I read from this documentation, it seems that you need to call the keep() function at the end of your code to allow mouse interaction. By the way the module seems to work only for python 2.7, which is too bad.
Reply
#3
Thank you very much (no irony!). As I said before, I just started coding (after a loong while..) and used the first IDE I stumbled about.
After reading your post I switched to python 3.7.1, using Thonny as IDE. Now I will try it again, hopefully with better results.
Reply
#4
Done. Now it works.
Reply
#5
Does gpanel work with python 3.7?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020