def splitWall(): if intPt is None: return 0 # print(w1, w2, intPt) dist = min(pv.mod(pv.vDiff(w1.start, intPt)), pv.mod(pv.vDiff(w1.end, intPt))) if dist < 0.1: return 0 split1 = wall(w1.start, intPt, owner=self) split2 = wall(intPt, w1.end, owner=self) # print('added 2 walls') return 1
def updateNeighbors(self): self.nbrs = list() midPt = pv.vPrd(pv.vSum(self.start, self.end), 0.5) ln = pv.unitV(pv.vDiff(self.end, self.start)) step = pv.vRotate(ln, math.pi / 2) pt1 = pv.vSum(midPt, step) pt2 = pv.vDiff(midPt, step) test = False for label in self.owner.c: if self.owner.c[label].hasPoint( pt1) or self.owner.c[label].hasPoint(pt2): self.nbrs.append(self.owner.c[label]) test = True
def scoreWith(self, anotherReg, stochasticity): scDiff = self.scaleDiff(anotherReg) if scDiff >= 0 and (not anotherReg is self.parent): scoreVal = anotherReg.size #making the score proportional to the size of the region d = pv.mod(pv.vDiff(self.center, anotherReg.center)) if d == 0:d = 1 #this is just to prevent division by zero scoreVal /= math.pow(d, scDiff + 1) #scoreVal *= anotherReg.type.agglomerationFactor scoreVal *= (1+(random.uniform(-1,1)*stochasticity)) for typeName in regType: self.score[typeName] += regType[typeName].rel(anotherReg.type)*scoreVal
def intercept(self, lineObj): #corners of the sqaure region sq = list() sq.append([self.pos[0]+self.size, self.pos[1]]) sq.append([self.pos[0]+self.size, self.pos[1]+self.size]) sq.append([self.pos[0], self.pos[1]+self.size]) sq.append(self.pos) iSum = 0 #this is the sum of intercepts that we will continue to increment i = 0 while i < len(lineObj.point)-1: #print(i, iSum) p1 = lineObj.point[i] p2 = lineObj.point[i+1] if self.hasPoint(p1) and self.hasPoint(p2): iSum += pv.mod(pv.vDiff(p2,p1)) else: s = 0 intPt = list() while s < len(sq) and len(intPt) <= 2: s1 = sq[s] s2 = sq[(s+1)%4] iPt = pv.intersectionPt(p1, p2, s1, s2) if not iPt is None: intPt.append(iPt) s += 1 if len(intPt) >= 2: iSum += pv.mod(pv.vDiff(intPt[0], intPt[1])) i += 1 return iSum
def hasPoint(self, pos):#this method returns a boolean whether pos lies inside this fence or not crossCount = 0 #counting the number of times the polugon is crossed rayVec = [1,0] # I am about to write a ray casting algorithm to the right i = 0 while i < len(self.vertex): v1 = self.vertex[i] v2 = self.vertex[(i+1)%len(self.vertex)] lineVec = pv.unitV(pv.vDiff(v2,v1)) lineCross = pv.vCross(rayVec, lineVec) if lineCross == 0: #the lines are parallel i += 1 # the increment of i is neccessary because this is a while loop not for loop # continue statement doesnot automatically do it, which I thought it does at first continue othCross = pv.vCross(pv.vDiff(v1,pos),lineVec) param = othCross/lineCross #checking if the point is on the perimeter if param == 0: return True elif param > 0: #checking if atleast one of the vertices lie below the threshold (look up oneNote notebook) if v1[1] < pos[1] or v2[1] < pos[1]: crossCount += 1 i += 1 if crossCount%2 == 0: return False else: return True
def minDistFrom(self, pos): i = 0 minDist = math.inf while i < len(self.point)-1: a = self.point[i] b = self.point[i+1] dt = pv.lineDist(a,b,pos) pt = pv.vSum(pos, dt) if pv.dot(pv.vDiff(pt,a),pv.vDiff(pt,b)) <= 0: distance = pv.mod(dt) else: dtA = pv.mod(pv.vDiff(pos,a)) dtB = pv.mod(pv.vDiff(pos,b)) distance = min(dtA, dtB) if distance < minDist: minDist = distance i += 1 return minDist
def render(self, img): draw = ImageDraw.Draw(img) # offset the door from ends of wall to avoid awkward joints offset = math.ceil((self.size / 2) + 1) offsetRatio = offset / (pv.mod(pv.vDiff(self.wall.end, self.wall.start))) # these are the start and end points of the wall after accounting for the offset newStart = pv.linEval(self.wall.start, self.wall.end, offsetRatio) newEnd = pv.linEval(self.wall.end, self.wall.start, offsetRatio) # now evaluating door position between these new start and new end points xypos = pv.linEval(newStart, newEnd, self.pos) # draw the door hSize = self.size / 2 coord = [ xypos[0] - hSize, xypos[1] - hSize, xypos[0] + hSize, xypos[1] + hSize ] draw.rectangle(coord, self.color) del draw return img
def length(self): return pv.mod(pv.vDiff(self.start, self.end))