【问题标题】:classify the set of polygons from side lengths in JavaScript在 JavaScript 中根据边长对多边形集进行分类
【发布时间】:2017-06-26 10:43:00
【问题描述】:

我有一个文件,其中包含以逗号分隔的边长列表(例如,“3,4,8,5,7”表示每行一个多边形)。

在 JavaScript 中,我如何将一组多边形分类为四个互斥的子集:三角形、矩形、正方形和其他所有子集。所有四个子集的并集应该是原始多边形集。多边形的所有边都是相连的,它们之间的角度无关紧要,只考虑长度。

【问题讨论】:

  • 我能够解析来自文件的输入,将其存储在数组数组中,例如:[[1,2,3],[1,2,1,2], [2,2,2,2]] 其中数组的每个索引代表多边形长度(子数组)并打印多边形的类型。
  • 请告诉我这是否是正确的做法。

标签: javascript node.js file


【解决方案1】:

您可以为您的整个问题找到解决方案here at my Github repo

虽然我在下面提供解决方案:

/**
 * count a value how many times in an array
 */
function countValueInArray(array, niddle) {
    return array.filter(item => item == niddle).length;
}

/**
 * read file 
 */
var fileText = [];

function readTextFile(file) {
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function() {
        if (rawFile.readyState === 4) {
            if (rawFile.status === 200 || rawFile.status == 0) {
                // when text file content json : [["1,3,1"], ["2,2,2,2"], ["2,4,2,4"], ["4,2,3"], ["3,4,2,6"]]
                //fileText = JSON.parse(rawFile.responseText);

                // for line separated file
                fileText = rawFile.responseText.split('\n');
                //alert(fileText);
            }
        }
    }
    rawFile.send(null);
}

/**
 * this will check whether it can make a triangle
 */
function checkTriangle(set) {
    if (set.length != 3) {
        return false;
    }
    var a = parseInt(set[0]);
    var b = parseInt(set[1]);
    var c = parseInt(set[2]);

    if (((a + b) > c) && ((a + c) > b) && ((b + c) > a)) {
        return true;
    } else {
        return false;
    }
}

/**
 * this will check whether it can make a square
 */
function checkSquare(set) {
    if (set.length != 4) {
        return false;
    }

    var a = parseInt(set[0]);
    var b = parseInt(set[1]);
    var c = parseInt(set[2]);
    var d = parseInt(set[3]);

    var equal = set.reduce(function(sum, val) {
        return val === set[0];
    });

    return equal;
}

/**
 * this will check whether it can make rectangle or not
 */
function checkRectangle(set) {
    if (set.length != 4) {
        return false;
    }

    var a = parseInt(set[0]);
    var b = parseInt(set[1]);
    var c = parseInt(set[2]);
    var d = parseInt(set[3]);

    if (((countValueInArray(set, a) == 2) && (countValueInArray(set, b) == 2)) || ((countValueInArray(set, c) == 2) && (countValueInArray(set, d) == 2)) || ((countValueInArray(set, a) == 2) && (countValueInArray(set, c) == 2)) || ((countValueInArray(set, a) == 2) && (countValueInArray(set, d) == 2)) || ((countValueInArray(set, b) == 2) && (countValueInArray(set, d) == 2)) || ((countValueInArray(set, b) == 2) && (countValueInArray(set, c) == 2))) {
        return true;
    } else {
        return false;
    }

}

// create polygons array
var polygons = [];
readTextFile('./polygons.txt');
//document.write(fileText.length);
fileText.forEach(function(p) {
    polygons.push(p);
});
//document.write(polygons[0]);
var triangles = [];
var squares = [];
var rectangles = [];
var others = [];
var result;
// polygons loop of all sets
for (let i = 0; i < polygons.length; i++) {
    var set;
    set = polygons[i].split(',');
    //document.writeln(set.length);

    if (set.length == 3) {
        result = checkTriangle(set);
        if (result == true) {
            triangles.push(set);
        } else {
            others.push(set);
        }

    } else if (set.length == 4) {
        result = checkSquare(set);
        if (result == true) {
            squares.push(set);
            //rectangles.push(set);
        } else if (checkRectangle(set) == true) {
            rectangles.push(set);
        } else {
            others.push(set);
        }

    } else {
        others.push(set);
    }
}

/*let unionOfAll = new Set(triangles, squares, rectangles, others);
unionOfAll.forEach(function(set){
    document.writeln(set);
});*/
// as they are already mutually exclusive set/array
let unionOfAll = triangles.concat(squares.concat(triangles.concat(others)));

document.write("Original polygons txt file contents: <br>" + fileText.join("<br>") + "<br><hr><br>");

document.write("Mutually exclusive sets:-<br>");
document.write("Triangles: " + JSON.stringify(triangles) + "<br>");
document.write("Squares: " + JSON.stringify(squares) + "<br>");
document.write("Rectangles: " + JSON.stringify(rectangles) + "<br>");
document.write("Others: " + JSON.stringify(others) + "<br><hr><br>");

document.write("Union of all: " + JSON.stringify(unionOfAll) + "<br>");

您必须创建一个多边形文本文件,其内容如下:

1,3,1 2,2,2,2 4,5,6 2,4,2,4 4,2,3 3,4,2,6 8,8,8,8 5,5,6,6 2,4,3,5,4

【讨论】:

    【解决方案2】:

    简单地除以 ,得到长度:

    var polygons={};
    ["1,3,1", "2,2,2,2"]. forEach(function(p){
      p=p.split(",");
     (polygons[p.length]=polygons[p.length]||[]).push(p);
    });
    

    现在您可以像这样访问它:

    polygons[3]//all triangles: [[1,3,1]]
    

    【讨论】:

    • 感谢 jonas 的代码,有没有一种通用方法可以将长度作为输入,进行一些计算并判断它是什么类型的多边形。
    • @bluestacksadmin 我不明白你的问题。只需使用带有名称的数组,例如 [undefined,"line","triangle","re​​ctangle","pentagon"]
    猜你喜欢
    • 2014-05-29
    • 2012-06-05
    • 2022-12-10
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    相关资源
    最近更新 更多