在js中找到多边形的中心点 [英] find center point of a polygon in js

查看:3713
本文介绍了在js中找到多边形的中心点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个不错的示例如何找到多边形的中心点(这里的JS ):



- >查看 this jsfiddle 示例



所以,用这个多边形

  var polygon = [
{'x':770,'y':400 },
{'x':529,'y':643},
{'x':320,'y':494},
{'x':424, y':381},
{'x':459,'y':369}
];

我应该找到这样的中心点:

  var con = new Contour(); 
con.pts = polygon;
document.write(con.centroid)

质心未定义



我做错了什么?

你调用 Contour

构造函数方法之前定义 Contour 原型。在所说的jsfiddle中,将你的 document.write 移动到结尾,并且所有都会...更好。



此外,您需要真正调用您定义的 centroid 函数:

  var c = con.centroid(); 
document.write(c.x);
document.write(c.y);


I have found a nice example how to find the center point of a polygon (and here in JS):

-> See this jsfiddle example

So, with this polygon

var polygon = [
     {'x':770, 'y':400},
     {'x':529, 'y':643},
     {'x':320, 'y':494},
     {'x':424, 'y':381},
     {'x':459, 'y':369}
];

I should find the center point like so:

var con = new Contour();
    con.pts = polygon;
    document.write(con.centroid)

But con.centroid is undefined.

What am I doing wrong? Thanks in advance!

解决方案

You call the Contour constructor way before the Contour prototype has been defined. In the said jsfiddle, move your document.write to the end, and all will go... better.

Also, you need to actually call the centroid function you defined:

var c = con.centroid();
document.write( c.x );
document.write( c.y );

这篇关于在js中找到多边形的中心点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆