
def joinURL(base, uri):
    if base.startswith("/"):
        if uri.startswith("/"):
            return base + uri[1,]
        else:
            return base + uri
    else:
        if uri.endswith("/"):
            return base + uri
        else:
            return base + "/" + uri


def spliteNName(dir):
    tdir = dir.lstrip("/")
    i = tdir.index("/")
    if i < 0:			#means just have nodename like "/{nodename}"
        return [tdir, "/"]
    else:
        return [tdir[:i], tdir[i+1:]]





class Point():
	def __init__(self, x, y):
		self.x=x
		self.y=y

	def Equal(self, point):
		if point.x==self.x and point.y==self.y:
			return True
		else:
			return False

	def Distance(self, point):
		xdistance=self.x-point.x
		ydistance=self.y-point.y
		return (xdistance*xdistance+ydistance*ydistance)**0.5


class Line():
    def __init__(self, startx, starty, endx, endy):
        self.start = Point(startx,starty)
        self.end = Point(endx,endy)

    def Length(self):
        xdistance=self.start.x-self.end.x
        ydistance=self.start.y-self.end.y
        return (xdistance*xdistance+ydistance*ydistance)**0.5

    def LineXCross(self, line):
        selfv = (self.start.x == self.end.x)         #1. 判断自己和目标线是横线还是竖线
        selfh = (self.start.y == self.end.y)
        linev = (line.start.x == line.end.x)
        lineh = (line.start.y == line.end.y)

        if selfh and linev:                         #2.自己是横线，目标是竖线，则计算目标x是否在自己两个x点之间 自己y是否在目标两个y点之间。(自己的两个端点不能算在内)
            if (line.start.x >= self.start.x and line.start.x <= self.end.x) or (line.start.x <= self.start.x and line.start.x >= self.end.x) :
                if (self.start.y >= line.start.y and self.start.y <= line.end.y) or (self.start.y <= line.start.y and self.start.y >= line.end.y) :
                    return Point(line.start.x, self.start.y)
        elif selfv and lineh:                       #3.自己是竖线，目标是横线，则计算
            if (line.start.y >= self.start.y and line.start.y <= self.end.y) or (line.start.y <= self.start.y and line.start.y >= self.end.y) :
                if (self.start.x >= line.start.x and self.start.x <= line.end.x) or (self.start.x <= line.start.x and self.start.x >= line.end.x) :
                    return Point(self.start.x, line.start.y)
            
        return None

    
    def PointXCross(self, point, angle):
        selfv = (self.start.x == self.end.x)         #1. 判断自己是横线还是竖线
        selfh = (self.start.y == self.end.y)

        if selfh :                                   #2.自己是横线
            if (point.x >= self.start.x and point.x <= self.end.x) or (point.x >= self.end.x and point.x <= self.start.x) :
                if (angle == 90 and point.y <= self.start.y) or (angle == 270 and point.y >= self.start.y) :
                    return Point(point.x, self.start.y)
        elif selfv :                                #3.自己是竖线
            if (point.y >= self.start.y and point.y <= self.end.y) or (point.y >= self.end.y and point.y <= self.start.y) :
                if (angle == 0 and point.x <= self.start.x) or (angle == 180 and point.x >= point.start.x) :
                    return Point(self.start.x, point.y)

        return None

    
    def PointInLine(self, point):
        selfv = (self.start.x == self.end.x)         #1. 判断自己是横线还是竖线
        selfh = (self.start.y == self.end.y)

        if selfh :                                   #2.自己是横线
            if (point.x >= self.start.x and point.x <= self.end.x) or (point.x >= self.end.x and point.x <= self.start.x) :
                return point.y == self.start.y
            
        elif selfv :                                #3.自己是竖线
            if (point.y >= self.start.y and point.y <= self.end.y) or (point.y >= self.end.y and point.y <= self.start.y) :
                return point.x == self.start.x

        return False
        

class Border(Line):
    def __init__(self, startx, starty, endx, endy, startthrough, endthrough):
        self.start = Point(startx,starty)
        self.end = Point(endx,endy)
        self.startthrough = startthrough        #border的起始点，是否可以穿行（90度角不可穿行，270度角可穿行）
        self.endthrough = endthrough            #border的终止点，是否可以穿行


    def AvailableLineXCross(self, line):
        xc = self.LineXCross(line)
        if xc == None:
            return None

        if self.startthrough and xc.Equal(self.start):     #如果边线起点可穿行，而交叉点正是起点，则不认为是有效交点
            return None
        elif self.endthrough and xc.Equal(self.end):     #如果边线终点可穿行，而交叉点正是终点，则不认为是有效交点
            return None

        return xc

    
    def AvailablePointXCross(self, point, angle):
        xc = self.PointXCross(point, angle)
        if xc == None:
            return None

        if self.startthrough and xc.Equal(self.start):     #如果边线起点可穿行，而交叉点正是起点，则不认为是有效交点
            return None
        elif self.endthrough and xc.Equal(self.end):     #如果边线终点可穿行，而交叉点正是终点，则不认为是有效交点
            return None

        return xc

    
    def AvailablePointInLine(self, point):
        if not self.PointInLine(point):
            return False

        if self.startthrough and point.Equal(self.start):
            return False
        elif self.endthrough and point.Equal(self.end):
            return False

        return True
        

class AvailablePoint(Point):       #表示点是否可穿行，主要用于线段交点
    def __init__(self, x, y, available):
        self.x=x
        self.y=y
        self.available=available
    

class Position():
    def __init__(self, point, angle):
        self.point = Point(point.x, point.y)
        self.angle = angle

    def Forward(self):
        if self.angle == 0 :
            self.point.x += 1
        elif self.angle == 90 :
            self.point.y += 1
        elif self.angle == 180 :
            self.point.x -= 1
        elif self.angle == 270 :
            self.point.y -= 1

    def Left(self):
        if self.angle == 270 :
            self.angle = 0
        else:
            self.angle += 90
        
    def Right(self):
        if self.angle == 0 :
            self.angle = 270
        else:
            self.angle -= 90
    
    def Back(self):
        if self.angle == 0 :
            self.angle = 180
        elif self.angle == 90 :
            self.angle = 270
        else:
            self.angle -= 180
    
