2011-04-15 59 views
6

我有一个矩形(称为目标),并希望放置另一个矩形(称为卫星)旁边。该卫星有一个位置(顶部,底部,左侧,右侧),用于确定相对于目标的放置边缘。它还有一个对齐方式(左,中,右为顶部和底部位置,顶部,中部和底部为左右位置)。优雅的方式来定位两个矩形

实施例:

+----------+----------------------------+ 
|   |       | 
| Target | Satellite, Position=RIGHT, | 
|   | Align=TOP     | 
|   |       | 
|   |----------------------------+ 
|   | 
+----------+ 

我知道左上坐标的目标以及其宽度和高度。我也知道卫星的宽度和高度,并且想要计算它的左上角坐标。我可以将它作为一系列12 if从句,但也许有更优雅的数学或算法的方法来完成它。有没有其他的方法来解决这个问题:

# s = satellite, t = target 
if pos == "top" && align == "left" 
    s.x = t.x 
    s.y = t.y - s.height 
else if pos == "top" && align == "center" 
    s.x = t.x + t.width/2 - s.width/2 
    s.y = t.y - s.height 
# etc, etc 

Ruby或JavaScript的任何好的解决方案?

+0

哥们,好文图!你有没有使用任何特殊的工具来生成它? – bowsersenior 2011-04-15 22:43:19

+0

是的,我用JavE,一个专门的ASCII图编辑器:http://www.jave.de/ – chiborg 2011-04-16 00:33:58

+0

男人你为什么不把'language-agnostic'标签? – 2011-04-16 04:37:35

回答

0

如果您使用的一系列对象会做的伎俩:

 
var positions = { 

    top: {left:{x:t.x, y:y.y-s.height}, center:{x:tx.x + t.width/2- s.width/2, y:t.y-s.height}} 
    //etc.... 
} 
//then to get the position you can simply 
var pos = positions[pos][align]) 

+0

这在功能上等同,难以阅读 – Wes 2011-04-15 21:06:50

1

我喜欢对方的回答,但这里是如何做到这一点,而不必存储任何。所有使用javascript true评估为1且false在算术运算符应用时评估为0的技巧的所有数学和逻辑:

p .s. (检查工作的jsfiddle:http://jsfiddle.net/vQqSe/52/

var t = { 
    jq: $('#target'), 
    width: parseInt($('#target').css('width')), 
    height: parseInt($('#target').css('height')), 
    top: parseInt($('#target').css('top')), 
    left: parseInt($('#target').css('left')) 
}; 
var s = { 
    jq: $('#satellite'), 
    width: parseInt($('#satellite').css('width')), 
    height: parseInt($('#satellite').css('height')) 
}; 

// start with it top left and add using javascript tricks 
s.jq.css('top', t.top - s.height + 
    s.height * (a == 'top') + 
    (t.height/2 + s.height/2) * (a == 'middle') + 
    t.height * (a == 'bottom') + 
    (t.height + s.height) * (p == 'bottom') 
);   

s.jq.css('left', t.left - s.width + 
    t.width * (a == 'left') + 
    s.width * (a == 'right') + 
    (s.width/2 + t.width/2) * (a == 'center') + 
    (s.width + t.width) * (p == 'right') 
); 
0
def vector pos, align, hash 
    case hash[pos] 
    when -1;  [0.0, -1.0] 
    when 1;  [1.0, 0.0] 
    else 
    case hash[align] 
    when -1; [0.0, 0.0] 
    when 1; [1.0, -1.0] 
    else  [0.5, -0.5] 
    end 
    end 
end 

y_t, y_s = vector(pos, align, "top" => -1, "bottom" => 1) 
x_t, x_s = vector(pos, align, "left" => -1, "right" => 1) 
s.y = t.y + y_t*t.height + y_s*s.height 
s.x = t.x + x_t*t.width + x_s*s.width 

def vector pos, align, head, tail 
    case pos 
    when head; [0.0, -1.0] 
    when tail; [1.0, 0.0] 
    else 
    case align 
    when head; [0.0, 0.0] 
    when tail; [1.0, -1.0] 
    else  [0.5, -0.5] 
    end 
    end 
end 

y_t, y_s = vector(pos, align, "top", "bottom") 
x_t, x_s = vector(pos, align, "left", "right") 
s.y = t.y + y_t*t.height + y_s*s.height 
s.x = t.x + x_t*t.width + x_s*s.width 
相关问题