﻿"""Provides a scripting component.
    Inputs:
        x: The x script variable
        y: The y script variable
    Output:
        a: The a output variable"""

__author__ = "joyce"
__version__ = "2023.04.07"

import rhinoscriptsyntax as rs
import random
""" print to output """
print("hello world")

""" function to calculate distance between two points"""
def dist2(p0, p1):
  return (p0.X- p1.X)*(p0.X- p1.X) + (p0.Y- p1.Y)*(p0.Y- p1.Y)+(p0.Z- p1.Z)*(p0.Z- p1.Z)

p1.X = w
p1.Y = h

""" create a list of points """
pt = []
dx = (p2.X-p1.X)/(rx-1)
dy = (p2.Y-p1.Y)/(ry-1)
print(dx, dy)
#ry = int(0.5+(p2.Y-p1.Y)/dx)
for i in range(0,ry):
    for j in range(0,rx):
        x = p1.X + j * dx
        y = p1.Y+ i*dy
        pt.append(rs.CreatePoint(x,y,0.0))
a = pt

""" create a list of lines """
ln = []
crv = []
crv2 = []
existingh = []
existingv = []

for j in range(0,len(pt)):
    for i in range(0,len(pt)):
        #check horizontal
        if(pt[i].Y == pt[j].Y and dist2(pt[i],pt[j])>(dx-.1) and pt[i] != pt[j]):
            #check border
            if(pt[i].Y > p1.Y-.1 or pt[i].Y < p2.Y+.1):
                crv.append(rs.AddLine(pt[i],pt[j]))
            else:
                r = random.choice([-1,1])
                print(r*dy)
                c2 = rs.CreatePoint(pt[i].X - (dx/3), pt[i].Y, (float)(pt[i].Z))
                c3 = rs.CreatePoint(pt[i].X - (dx/3), pt[i].Y + (r*dy/5), (float)(pt[i].Z))
                c4 = rs.CreatePoint(pt[i].X - (2*dx/3), pt[i].Y + (r*dy/5), (float)(pt[i].Z))
                c5 = rs.CreatePoint(pt[i].X - (2*dx/3), pt[i].Y, (float)(pt[i].Z))
                n = rs.AddInterpCurve([pt[i], c2, c3, c4, c5, pt[i-1]]) 
                #checks length of curve to filter wrapping curves
                if(rs.CurveLength(n) < w and not(i in existingh)):
                    crv.append(n)
                    existingh.append(i)
        #check vertical
        if(pt[i].X == pt[j].X and dist2(pt[i],pt[j])>(dy-.1) and pt[i] != pt[j]):
            #check border
            if(pt[i].X > p1.X-.1 or pt[i].X < p2.X+.1):
                crv.append(rs.AddLine(pt[i],pt[j]))
            else:
                r = random.choice([-1,1])
                c2 = rs.CreatePoint(pt[j].X , pt[j].Y- (2*dy/3), (pt[j].Z))
                c3 = rs.CreatePoint(pt[j].X - r*(dx/5), pt[j].Y - (2*dy/3),(pt[j].Z))
                c4 = rs.CreatePoint(pt[j].X - r*(dx/5), pt[j].Y - (dy/3), (pt[j].Z))
                c5 = rs.CreatePoint(pt[j].X, pt[j].Y - (dy/3), (pt[j].Z))
                n = rs.AddInterpCurve([pt[j-rx], c2, c3, c4, c5, pt[j]]) 
                #checks length of curve to filter wrapping curves
                if(rs.CurveLength(n) < h and not(j in existingv)):
                    crv.append(n)
                    existingv.append(j)
 
c=crv



