2015-06-20 102 views
0

我有一個直角平面有固定尺寸邊緣檢查座標系

WIDTH, HEIGHT = 10, 20 

我想,以確定是否表示爲有序對的點是邊緣或該平面的拐角。例如,[0, 0][9, 19]是轉角; [0, 5][6, 19]是邊緣。

這是我的代碼:

# max allowable x and y coordinates 
MAXX = WIDTH - 1 
MAXY = HEIGHT - 1 

# is this coordinate at the corner of the plane? 
def corner?(*coords) 
    coords.uniq == [0]  || # lower-left corner 
    coords == [MAXX, MAXY] || # upper-right corner 
    coords == [MAXX, 0] || # lower-right corner 
    coords == [0, MAXY]  # upper-left corner 
end 

# is this coordinate at the edge of the plane? 
def edge?(*coords) 
    return (
    (
     coords.include?(0) || # if x or y coordinate is at 
     coords[0] == MAXX || # its min or max, the coordinate 
     coords[1] == MAXY  # is an edge coordinate 
    ) && 
    !corner?(coords) # it's not an edge if it's a corner 
) 
end 

它提供了這些結果,這是我所期望的:

corner?(0, 0) #=> true 
corner?(0, 5) #=> false 
edge?(0, 5) #=> true 
corner?(5, 5) #=> false 
edge?(5, 5) #=> false 

然而,當我想到以下幾點:

edge?(0, 0) #=> true 

它給

edge?(0, 0) #=> false 

我在做什麼錯?

回答

0

注意,在你的edge?方法,你在呼喚:

def edge?(*coords) 
    #... 
    !corner?(coords) 
end 

其中coords是一個Array,即,您呼叫corner?([0, 0]),不corner?(0, 0)。相反,展開這樣的參數:

def edge?(*coords) 
    #... 
    !corner?(*coords) 
end