简体   繁体   English

jQuery:排序4点到(左上,右上,右下,左下)

[英]jquery: sort 4 points to (top-left, top-right, bottom-right, bottom-left)

given: 2 arrays, each containing the coordinates of 4 points: 给定:2个数组,每个数组包含4个点的坐标:

point_array_a = [point_a_1_x, point_a_1_y, point_a_2_x, ..., point_a_4_y]
point_array_b = [point_b_1_x, ...                       ..., point_b_4_y]

task: 任务:

  1. sort point_array_a such that in the end the points are listed in the following order: 对point_array_a进行排序,以便最后按以下顺序列出点:

     point_array_a_sorted = [top-left_x, top_left_y, top-right_x, top-right_y, bottom-right_x, bottom_right_y, bottom-left_x, bottom_left_y] 
  2. sort point_array_b in the same way, such that point_a_k_l corresponds to point_b_k_l like in the beginning. 以相同的方式对point_array_b进行排序,以使point_a_k_l对应于开始时的point_b_k_l。

I'm afraid there is no simple algorithm for this. 恐怕没有简单的算法可以做到这一点。 But the following snippet will do the job (assuming that points with larger y-coordinates lie lower than points with lower y-coordinates): 但是下面的代码片段就可以了(假设y坐标较大的点低于y坐标较低的点):

var i, points = [], leftX = point_array_a[0], topY = point_array_a[1];

for (i = 0; i < 4; i++)
{
    leftX = Math.min(leftX, point_array_a[i * 2]);
    topY = Math.min(topY, point_array_b[i * 2]);
    points.push([
                 [point_array_a[i * 2], point_array_a[i * 2 + 1]],
                 [point_array_b[i * 2], point_array_b[i * 2 + 1]]
                ]);
}

points.sort(function(first, second){
    if (first[0][0] == leftX)
        return first[0][1] == topY ? -1 : 1;
    if (second[0][0] == leftX)
        return second[0][1] == topY ? 1 : -1;
    return first[0][1] < second[0][1] ? -1 : 1;
});

var point_array_a_sorted = [], point_array_b_sorted = [];

for (i = 0; i < 4; i++)
{
    point_array_a_sorted.push(points[i][0][0], points[i][0][1]);
    point_array_b_sorted.push(points[i][1][0], points[i][1][1]);
}

We leverage the existing Array.sort function by feeding it just the right objects to compare and swap — pairs of points. 我们通过为现有的Array.sort函数提供正确的对象来进行比较和交换(成对的点)来利用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery Border Image(左上,右上,左下,右下) - jQuery Border Image (top-left, top-right, bottom-left, bottom-right) 如何在mousemove事件(左上,右上,左下和右下)上确定鼠标的方向 - How to determine the direction of mouse on mousemove event (top-left, top-right, bottom-left and bottom-right) react-spring:从左上角到右下角淡入淡出 - react-spring: fade in top-left to bottom-right 如何从边界值中提取左上角和右下角坐标 - How to extract top-left and bottom-right coordinates from bounds value 给定以矩形左下角为中心的圆,计算圆的边界矩形的宽度,使其半径接触右上角 - Given circle that is centered on bottom-left corner of a rect, calculate width of circle's bounding rect so its radius touches the top-right corner Galleria滑块从左到右从上到下 - Galleria slider from left—right to top—bottom getBoundingClientRect 在 Safari 中为顶部、左侧、右侧、底部返回 0 - getBoundingClientRect return 0 for top, left, right, bottom in Safari jQuery UI Position设置为“ right”和“ bottom”,而不是“ left”和“ top” - jQuery UI Position to set “right” and “bottom” instead of “left” and “top” jQuery OnMouseMove滚动div上/下,不是右/左,怎么样? - jQuery OnMouseMove scroll div Top/Bottom, NOT right/left, how? jquery marquee从上到下而不是从右到左制作卷轴 - jquery marquee make scroller from top to bottom instead of right to left
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM