javascript - 从由独立线创建的形状创建多边形

标签 javascript svg

我有一组 10 条线,它们被排列来创建一个形状。这些线是独立的(不连续)。我想通过查找顺序点来构建多边形,使其与形状匹配。有什么想法吗?

示例如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Create Polygon From Lines</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body style='padding:10px;font-family:arial'>
<center>
<h4>Create Polygon From Lines</h4>
<div style='width:90%;background-color:gainsboro;text-align:justify;padding:10px;border-radius:6px;'>
A group of independent lines are connected to form a shape. Create a polygon that matches the shape.
</div>
<table><tr>
<td>
<div style="padding:10px;width:400px;text-align:justify">

<b>Scenerio:</b><br />
The shape is shown dashed:
Lines have been randomly placed and are not contiguous.<br>
We must arrange the points so a polygon can be drawn that matches the shape, i.e, the points must be sequential.
</div>
</td>
<td>
<div id="svgDiv" style='background-color:lightgreen;width:400px;height:400px;'>
<svg id="mySVG" width="400" height="400" overflow="visible">
<g id=lineG fill="none" stroke=black stroke-width=1 stroke-dasharray="4 4" />
<polygon id=myPolygon stroke='blue' fill='none' stroke-width='3' />
</svg>
</div>
</td>
</tr></table>
 <script>
var Lines=[]  //---[x1,y1,x2,y2]---
Lines[0]=[170, 148, 140, 200]
Lines[1]=[140, 200, 170, 251]
Lines[2]=[230, 251, 260, 200]
Lines[3]=[170, 148, 200, 96]
Lines[4]=[230, 251, 200, 303]
Lines[5]=[200, 303, 170, 251]
Lines[6]=[281, 118, 251, 66]
Lines[7]=[251, 66, 200, 96]
Lines[8]=[281, 118, 311, 170]
Lines[9]=[311, 170, 260, 200]

var NS="http://www.w3.org/2000/svg"
//---onLoad---
function placeLines()
{
    for(var k=0;k<Lines.length;k++)
    {
       var line=document.createElementNS(NS,"line")
       line.setAttribute("x1",Lines[k][0])
       line.setAttribute("y1",Lines[k][1])
       line.setAttribute("x2",Lines[k][2])
       line.setAttribute("y2",Lines[k][3])
       lineG.appendChild(line)
    }
}

document.addEventListener("onload",placeLines(),false)

</script>
</body>
</html>

最佳答案

您可以使用 polygon具有属性。在下面的示例中,我只是制作了一个正方形,但您可以根据需要修改点数组...

另请注意,我将其修改为使用 array.map() - 这种方式比创建变量、使用该变量作为数组中的索引,然后在每次迭代后递增它更简单。欲了解更多信息,请阅读these exercises .

<center>
<h4>Create Polygon From Lines</h4>
<div style='width:90%;background-color:gainsboro;text-align:justify;padding:10px;border-radius:6px;'>
A group of independent lines are connected to form a shape. Create a polygon that matches the shape.
</div>
<table><tr>
<td>
<div style="padding:10px;width:200px;text-align:justify">

<b>Scenerio:</b><br />
The shape is shown dashed:
Lines have been randomly placed and are not contiguous.<br>
We must arrange the points so a polygon can be drawn that matches the shape, i.e, the points must be sequential.
</div>
</td>
<td>
<div id="svgDiv" style='background-color:lightgreen;width:400px;height:400px;'>
<svg id="mySVG" width="400" height="400" overflow="visible">
<g id="shapeG" fill="none" stroke=black stroke-width=1 stroke-dasharray="4 4" />
<polygon id=myPolygon stroke='blue' fill='none' stroke-width='3' />
</svg>
</div>
</td>
</tr></table>
 <script>
var points=[  //figure out the points for your polygon
     [100, 100], // this is a simple square
     [200, 100],
     [200, 200],
     [100, 200]
];
var NS="http://www.w3.org/2000/svg"
//---onLoad---
function placeLines() {
    var polygon=document.createElementNS(NS,"polygon");

    var pointsAttribute = points.map(function(point) {
        return point[0]+' '+point[1];
    }).join(', ');
    polygon.setAttribute("points",pointsAttribute);
    document.getElementById('shapeG').appendChild(polygon);
}

document.addEventListener("onload",placeLines(),false)

</script>

编辑:

您询问如何从线列表中计算连续点。我不确定执行此操作的最佳方法是什么,但一种方法是创建 linked-list点(也可以被视为图表)。看一下这个例子:

function Point(value) {
  this.value = value;
  this.next = null;
}
Point.prototype.SetNext = function(nextPoint) {
  this.next = nextPoint;
};

function PointList() {
  this.items = [];
}
PointList.prototype.addOrGet = function(value) {
  var foundPoint = this.items.find(function(item) {
    return item.value == value;
  });
  if (foundPoint) {
    return foundPoint;
  }
  //create new point
  var newPoint = new Point(value);
  this.items.push(newPoint);
  return newPoint;
};
PointList.prototype.GetPointsInOrder = function() {
  var current = this.items[0],
    next, nextPoints = [];
  for (var counter = 0; counter < this.items.length; counter++) {
    next = current.next;
    if (next) {
      nextPoints.push(next.value);
      current = next;
    }
  }
  return nextPoints.join(', ');
}

var Lines = [] //---[x1,y1,x2,y2]---
Lines[0] = [170, 148, 140, 200]
Lines[1] = [140, 200, 170, 251]
Lines[2] = [230, 251, 260, 200]
Lines[3] = [170, 148, 200, 96]
Lines[4] = [230, 251, 200, 303]
Lines[5] = [200, 303, 170, 251]
Lines[6] = [281, 118, 251, 66]
Lines[7] = [251, 66, 200, 96]
Lines[8] = [281, 118, 311, 170]
Lines[9] = [311, 170, 260, 200]

var NS = "http://www.w3.org/2000/svg"

function placeLines() {
  var placedLines = [];
  var polygon = document.createElementNS(NS, "polygon");
  var linePoints = [],
    point1, point2, indexOfPoint1, indexOfPoint2;
  var pointList = new PointList(),
    point1, point2, point1str, point2str;
  for (var k = 0; k < Lines.length; k++) {
    point1str = Lines[k][0] + ' ' + Lines[k][1];
    point2str = Lines[k][2] + ' ' + Lines[k][3];
    point1 = pointList.addOrGet(point1str);
    point2 = pointList.addOrGet(point2str);
    if (!point1.next) {
      point1.SetNext(point2);
    } else if (!point2.next) {
      point2.SetNext(point1);
    }
  }
  polygon.setAttribute("points", pointList.GetPointsInOrder());
  document.getElementById('lineG').appendChild(polygon);
}
document.addEventListener("onload", placeLines(), false);
<div id="svgDiv" style='background-color:lightgreen;width:400px;height:400px;'>
  <svg id="mySVG" width="400" height="400" overflow="visible">
    <g id=lineG fill="none" stroke=black stroke-width=1 stroke-dasharray="4 4" />
    <polygon id=myPolygon stroke='blue' fill='none' stroke-width='3' />
  </svg>
</div>

关于javascript - 从由独立线创建的形状创建多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40620946/

相关文章:

html - 在 CSS 中更改 SVG 描边颜色

javascript - Angular - 每 30 秒调用一次函数,但考虑运行该函数所需的时间

javascript - jQuery - 每当用户更改任何表单输入时调用一个函数?

html - 将 img.src 设置为动态 svg 元素

javascript - 如何根据度数和坐标绘制椭圆

css - 制作一个真正 flex 的容器,里面有图像

javascript - OData V2 模型上的 Kapsel 离线应用程序的 SAPUI5 深度插入

javascript - 移动设备上的 CSS 下拉菜单。如何绕过 :hover

javascript - 翻页效果如何在 turn.js 中工作

javascript - 我的 svg 路径使用 d3 进行像素化