jdf / processing-py-site

Site for processing.py.
MIT License
197 stars 52 forks source link

Same code on Jupyter Notebook and Python Processing generates different calculation results #212

Closed christophwagnergillen closed 2 years ago

christophwagnergillen commented 2 years ago

I have the following code on a Jupyter Notebook:

# Divide canvas in quadrants
bild_horizontal = 1920
bild_vertikal = 1080
randstreifen_horizontal = 64
randstreifen_vertikal = 64
schnitt=(bild_horizontal-randstreifen_horizontal)/(bild_vertikal-randstreifen_vertikal)
quadranten_varianten = [0, 3, 5, 7, 11, 13]
quadrant_startpunkt = []

# Calculate starting point for each quadrant
for a in range(len(quadranten_varianten)):
  quadranten_horizontal = int(quadranten_varianten[a])
  quadranten_vertikal = int(round((quadranten_horizontal/schnitt)))
  print (quadranten_horizontal)     
  print(quadranten_vertikal) 
  for b in range(quadranten_horizontal):
    for c in range(quadranten_vertikal):
      quadrant_x_startpunkt = int(round((bild_horizontal/quadranten_horizontal)*b))
      quadrant_y_startpunkt = int(round((bild_vertikal/quadranten_vertikal)*c))
      quadrant_startpunkt.append([quadranten_varianten[a], quadrant_x_startpunkt, quadrant_y_startpunkt])

Running the code on a Jupyter notebook provides the following results:

0 0 3 2 5 3 7 4 11 6 13 7

If the same code runs on Processing, the following results are generated:

0 0 3 3 5 5 7 7 11 11 13 13

Where is my mistake?

Thanks a lot for your help. I have spend hours without resolving the issue.

tabreturn commented 2 years ago

Python 2 vs Python 3 division. Processing.py uses Jython, which supports Python 2 --

print(5 / 2)  # displays 2 (but 2.5 in Python 3)
print(5.0 / 2)  # displays 2.5

You can convert your integers to floats --

bild_horizontal = 1920.
bild_vertikal = 1080.
...

Alternatively, you might import Python 3 division by adding this line to the top of your code --

from __future__ import division

print(5 / 2)  # displays 2.5
christophwagnergillen commented 2 years ago

Hello tabreturn,

Thanks a lot for the help. Very much appreciated. It works now perfectly :).