Ghpython Conditional Points Segregation

conditiooal points.gh (6.3 KB)
Hi All

I am a novice in ghpython and jsut started doing some tutorials. The one attached is where I got stuck at #16 of the script where it says

elif x%num1==0 or y%num2==0:

to my understanding this means if the x number of the loop is divided by num1 (in this case 6) and equals to 0, then append these points to pt2 variable. (same for y%num2)
The question is when the x values in the loop say when x=4 or 5, then 4%6=2,5%6=1 (pls correct me if I am wrong!!)
However, having said that, points x4, x5 are still being appeneded to pt2 (shown in red)
Please also see my mark up if it shows what I mean a bit better.

so can anyone guide me through as to why x4 and x5 are in list pt2 when Num1 is 6?
thank you so much!
regards

Yutaka

% is modulus, not division:

It helps to disable preview on the GhPython Script component.

import rhinoscriptsyntax as rs

import Rhino.Geometry as gh

pt1=[]
pt2=[]
pt3=[]

for x in range(0,100,1):
    for y in range(0,100,1):
        pt=rs.AddPoint(x,y,0)
        if x%num1==0 and y%num2==0:
            pt1.append(pt)
        elif x%num1==0 or y%num2==0: # but num1: 4%6=2,5%6=1 
            pt2.append(pt)
        else:
            pt3.append(pt)

Hi Ben thanks I am aware of that , yet 4%6 returns 4
The condition says if x%num-==0 append x to pt2

So can you see why x4 is appended to pt2 beocuse it’s modulo with 6 is not 0 and I can’t see why it is still appended to pt2.

Many thanks
Yutaka

It says:

elif x%num1==0 or y%num2==0: # but num1: 4%6=2,5%6=1 
            pt2.append(pt)

So y must be an even number, eh? (y%num2==0)

Maybe my coffee hasn’t kicked in yet?

I don’t see anything wrong?


conditiooal points_2023Mar8a.gh (7.6 KB)

When coding, I like to turn all the lights on, so to speak.


conditiooal points_2023Mar8b.gh (9.0 KB)

import rhinoscriptsyntax as rs
import Rhino.Geometry as gh

pt1=[]
pt2=[]
pt3=[]

ptsX=[]
for x in range(0,xSize,1):
    ptsY=[]
    for y in range(0,ySize,1):
        pt=rs.AddPoint(x,y,0)
        if y==0:
            ptsX.append(pt)
        ptsY.append(pt)
        if x%numX==0 and y%numY==0:
            pt1.append(pt)
        elif x%numX==0 or y%numY==0: # but num1: 4%6=2,5%6=1 
            pt2.append(pt)
        else:
            pt3.append(pt)

Or:

import rhinoscriptsyntax as rs
import Rhino.Geometry as gh

pt1=[]
pt2=[]
pt3=[]
ptsX=[]
ptsY=[]

for x in range(0,xSize,1):
    for y in range(0,ySize,1):
        pt=rs.AddPoint(x,y,0)
        if x==0:
            ptsY.append(pt)
        if y==0:
            ptsX.append(pt)
        if x%numX==0 and y%numY==0:
            pt1.append(pt)
        elif x%numX==0 or y%numY==0: # but num1: 4%6=2,5%6=1 
            pt2.append(pt)
        else:
            pt3.append(pt)

conditiooal points_2023Mar8c.gh (9.0 KB)

Hi Joseph

Thank you so much for your effort to show a neater way to code!
Everything is much clearer now,

just one thing, i didn’t quite understand what this meant (maybe I need a coffee too lol):

elif : x%numX==0 or y%numY==0:

Could you kindly explain?
Super thanks!!

Yutaka

Too early yet for my coffee but yours must have kicked in by now? :wink:

else if either condition is true

That’s “or” as opposed to “and” where both conditions must be true.

This is the same code, slightly refactored for readability:
(though longer code isn’t necessarily easier to read!)

import rhinoscriptsyntax as rs
import Rhino.Geometry as gh

pts_White = []
pts_Red = []
pts_Black = []

# trace points
ptsX=[]
ptsY=[]

for x in range(0,xSize,1):
    for y in range(0,ySize,1):
        pt=rs.AddPoint(x,y,0)
        X_evenly = x % numX == 0
        Y_evenly = y % numY == 0
        if X_evenly and Y_evenly:
            pts_White.append(pt)
        elif X_evenly or Y_evenly:
            pts_Red.append(pt)
        else:
            pts_Black.append(pt)

        # trace points
        if x==0:
            ptsY.append(pt)
        if y==0:
            ptsX.append(pt)


conditional points_2023Mar9a.gh (8.8 KB)

Thank you Joseph!

It’s kicking in now , so for example at the X axis where it marks 1, the reason why it is red is because the coordinate at that point is 1,0 (y is evenly) etc?

Thanks again!

I think you’ve got it, yes, but talking further might confuse the issue. Pretty obvious all along, eh?

Thank you so much, your help was the double shot expresso :laughing:, applause to you mister! :clap: :clap: :clap: