当前位置: 首页> 文旅> 酒店 > 精选资料_mac十大最好看色号_自己接单的平台_seo排名优化课程

精选资料_mac十大最好看色号_自己接单的平台_seo排名优化课程

时间:2025/8/10 3:28:43来源:https://blog.csdn.net/m0_46281382/article/details/144796620 浏览次数:0次
精选资料_mac十大最好看色号_自己接单的平台_seo排名优化课程

概述

在 Openlayers 中,Polygon类是SimpleGeometry的子类,用于表示多边形几何形状。多边形是由一组点组成的封闭图形,通常由一系列的坐标点和多个边界组成. 关于SimpleGeometry类,可以参考这篇文章源码分析之Openlayers中SimpleGeometry类

本文主要介绍Polygon类的源码实现和原理。

源码解析

Polygon类源码实现

Polygon类的源码实现如下:

class Polygon extends SimpleGeometry {constructor() {super();this.ends_ = [];this.flatInteriorPointRevsion_ = -1;this.flatInteriorPoint_ = null;this.maxDelta_=-1;this.maxDeltaRevision_ = -1;this.orientrevision_ = -1;this.orientedFlatCoordinates_ = null;if (layout !== undefined && ends) {this.setFlatCoordinates(layout, coordinates);this.ends_ = ends;} else {this.setCoordinates(coordinates, layout);}}appendLinearRing(linearRing) {if (!this.flatCoordinates) {this.flatCoordinates = linearRing.getFlatCoordinates().slice();} else {extend(this.flatCoordinates, linearRing.getFlatCoordinates());}this.ends_.push(this.flatCoordinates.length);this.changed();}clone() {const polygon = new Polygon(this.flatCoordinates.slice(),this.layout,this.ends_.slice());polygon.applyProperties(this);return polygon;}closestPointXY(x, y, closestPoint, minSquaredDistance) {if (minSquaredDistance < closestSquaredDistanceXY(this.getExtent(), x, y)) {return minSquaredDistance;}if (this.maxDeltaRevision_ != this.getRevision()) {this.maxDelta_ = Math.sqrt(arrayMaxSquaredDelta(this.flatCoordinates,0,this.ends_,this.stride,0));this.maxDeltaRevision_ = this.getRevision();}return assignClosestArrayPoint(this.flatCoordinates,0,this.ends_,this.stride,this.maxDelta_,true,x,y,closestPoint,minSquaredDistance);}containsXY(x, y) {return linearRingsContainsXY(this.getOrientedFlatCoordinates(),0,this.ends_,this.stride,x,y);}getArea() {return linearRingsArea(this.getOrientedFlatCoordinates(),0,this.ends_,this.stride);}getCoordinates(right) {let flatCoordinates;if (right !== undefined) {flatCoordinates = this.getOrientedFlatCoordinates().slice();orientLinearRings(flatCoordinates, 0, this.ends_, this.stride, right);} else {flatCoordinates = this.flatCoordinates;}return inflateCoordinatesArray(flatCoordinates, 0, this.ends_, this.stride);}getEnds() {return this.ends_;}getFlatInteriorPoint() {if (this.flatInteriorPointRevision_ != this.getRevision()) {const flatCenter = getCenter(this.getExtent());this.flatInteriorPoint_ = getInteriorPointOfArray(this.getOrientedFlatCoordinates(),0,this.ends_,this.stride,flatCenter,0);this.flatInteriorPointRevision_ = this.getRevision();}return this.flatInteriorPoint_;}getInteriorPoint() {return new Point(this.getFlatInteriorPoint(), "XYM");}getLinearRingCount() {return this.ends_.length;}getLinearRing(index) {if (index < 0 || this.ends_.length <= index) {return null;}return new LinearRing(this.flatCoordinates.slice(index === 0 ? 0 : this.ends_[index - 1],this.ends_[index]),this.layout);}getLinearRings() {const layout = this.layout;const flatCoordinates = this.flatCoordinates;const ends = this.ends_;const linearRings = [];let offset = 0;for (let i = 0, ii = ends.length; i < ii; ++i) {const end = ends[i];const linearRing = new LinearRing(flatCoordinates.slice(offset, end),layout);linearRings.push(linearRing);offset = end;}return linearRings;}getOrientedFlatCoordinates() {if (this.orientedRevision_ != this.getRevision()) {const flatCoordinates = this.flatCoordinates;if (linearRingsAreOriented(flatCoordinates, 0, this.ends_, this.stride)) {this.orientedFlatCoordinates_ = flatCoordinates;} else {this.orientedFlatCoordinates_ = flatCoordinates.slice();this.orientedFlatCoordinates_.length = orientLinearRings(this.orientedFlatCoordinates_,0,this.ends_,this.stride);}this.orientedRevision_ = this.getRevision();}return this.orientedFlatCoordinates_;}getSimplifiedGeometryInternal(squaredTolerance) {const simplifiedFlatCoordinates = [];const simplifiedEnds = [];simplifiedFlatCoordinates.length = quantizeArray(this.flatCoordinates,0,this.ends_,this.stride,Math.sqrt(squaredTolerance),simplifiedFlatCoordinates,0,simplifiedEnds);return new Polygon(simplifiedFlatCoordinates, "XY", simplifiedEnds);}getType() {return "Polygon";}intersectsExtent(extent) {return intersectsLinearRingArray(this.getOrientedFlatCoordinates(),0,this.ends_,this.stride,extent);}setCoordinates(coordinates, layout) {this.setLayout(layout, coordinates, 2);if (!this.flatCoordinates) {this.flatCoordinates = [];}const ends = deflateCoordinatesArray(this.flatCoordinates,0,coordinates,this.stride,this.ends_);this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1];this.changed();}
}

Polygon类的构造函数

Polygon类的构造函数接受三个参数:coordinates坐标点、layout坐标风格和ends边界点;另外构造函数中还有如下成员变量:

  • this.ends_:初始化为空数组[],用于存储多边形的边界信息。对于多边形,ends存储的是每一个环(环形边界)的结束坐标索引。

  • this.flatInteriorPointRevision:初始化为-1,用于记录多边形内部点的版本信息,主要是在多边形优化和缓存内部点计算时用到

  • this.flatInteriorPoint_:默认为null,用于缓存多边形的内部点或质心

  • this.maxDeltaREvision_:多边形形状更新的最大变化版本,和LinearRing类中的同名变量类似,可以参考源码分析之Openlayers中的LinearRing类

  • this.orientedRevision_:初始为-1,用于表示多边形的方向版本;如果多边形的坐标顺序发生变化,它将被更新

  • this.orientedFlatCoordiantes_:初始为null,用于存储有方向性的平面坐标

Polygon类的构造函数会判断,若参数layout坐标布局风格和环信息ends存在,则调用父类SimpleGeometry类中的this.setFlatCoordinates方法设置坐标;否则,调用this.setCoordiantes方法。

Polygon类的主要方法

Polygone类的主要方法如下:

  • setCoordiantes方法:用于设置坐标this.flatCoordiantes、坐标布局风格this.layout和步幅this.stride,最后会调用this.changed方法,该方法会修改this.revision_的值,后面几个方法内部调用this.getRevision()方法就是获取this.revision_的值,它们是在Observable类中定义的,可以参考这篇文章源码分析之Openlayers中的Observable类

  • intersectsExtent方法:该方法用于判断几何对象是否与extent交叉

  • getType方法:返回几何对象多边形的类型,Polygon

  • getSimplifiedGeometryInternal方法:获取简化后的几何对象,接受一个参数squaredTolerance容差值平方,内部就是调用quantizeArray方法简化几何对象,然后获取简化几何对象的坐标,再调用Polygon进行实例化,并返回

  • getOrientedFlatCoordiantes方法:返回多边形顶点的有向平面坐标;首先会判断多边形是否发生变化,若未变化,则直接返回this.orientedFlatCoordinates_;否则调用linearRingsAreOriented方法判断线性环坐标是否是有向的,若是,则直接返回原始坐标;否则复制一份原始坐标数组,然后调用orientLinearRings方法重新调整坐标的方向,确保环的方向正确,说白了就是会调整坐标的顺序,然后修改this.orientedFlatCoordintes_this.orientedRevision_

  • getLinearRing方法:根据索引值index获取某一个具体的线性环,首先会判断索引值index的合法性,然后实例化LinearRing类并返回实例对象。

  • getLinearRings方法:获取多边形的环,内部会遍历this.ends_,前面提过这个变量存储的是环的结束点坐标索引,即每个环的最后一个坐标在this.flatCoordinates中的位置,遍历this.ends_然后截取this.flatCoordiantes就可以得到每个环的坐标数组,然后调用LinearRing类实例化线性环,将其值保存到数组linearRings中,最后返回linearRings

  • getLinearRingCount方法:获取多边形环的个数

  • getInteriorPoint方法:方法内部会调用this.getFlatInteriorPoint()方法获取多边形内部的代表点,然后实例化Point类并返回实例对象。

  • getFlatInteriorPoint方法:获取几何对象的内部点,该方法主要用于计算并返回多边形的一个内部点(通常是内部几何中心点),并缓存该结果。它会在多边形发生变更时重新计算,并在未发生变化时直接返回缓存的结果。首先会判断几何对象是否更新变化过,若变化了,则调用getCenter方法计算几何对象包围盒的中心点,然后调用getInteriorPointOfArray计算出一个内部点-多边形内部的代表点并返回,将其赋值给this.flatInteriorPoint_变量,最后修改this.flatInteriorPointRevision_的值;若未变化,则返回this.flatInteriorPoint_

  • getEnds方法:获取变量this.ends_的值,即多边形中环的结束点索引位置数组

  • getCoordinates方法:获取几何对象的坐标数组

  • getArea方法:获取多边形面积,内部调用的是linearRingArea方法计算多边形面积,运用的是高斯面积公式

  • containsXY方法:用于判断给定点(x,y)是否在多边形内部或边界上,内部调用的是linearRingsContainsXY方法

  • closestPointXY方法:几何对象中很常见的方法了,给定一个目标点(x,y)、最近点坐标closestPoint和最小平方距离minSquaredDistance,然后计算多边形中距离目标点最近的点,并返回多边形距离目标点最近距离的平方;closestPointXY方法内部会先调用closestSquaredDistanceXY计算几何对象包围盒距离目标点最近的距离,若参数minSquaredDistance小于该距离则直接返回它;然后判断this.maxDeltaRevision_是否与this.getResvision()方法返回的变量相等,也就是判断几何对象多边形是否发生了改变,若发生了改变,就调用arrayMaxSquaredDelta方法来计算多边形顶点的最大坐标变化,并将其平方根保存在this.maxDelta变量中,最后更新this.maxDeltaRevision_为当前修订版本号;最后调用assignCloestArrayPoint方法来计算目标点(x,y)与多边形最近的点,并更新最近点坐标closestPoint和最小距离平方``minSquaredDistance`并返回最小距离平方.

  • clone方法:该方法内部就是调用Polygon类实例化一个多边形,然后调用实例的applyProperties方法应用属性,最后返回实例。

  • appendLinearRing方法:参数linearRing是一个LinearRing类的实例,该方法用于将线性环添加到多边形;首先会判断,若this.flatCoordiantes不存在,即多边形没有坐标点,则将调用线性环的getFlatCoordiantes方法获取它的坐标,赋值给this.flatCoordinates,否则调用extent方法,将线性环的坐标添加到this.flatCoordiantes中;更新变量this.ends,最后调用this.changed()方法

总结

本文主要介绍Polygon类的实现原理,LinearRing类是Polygon类的基础。

关键字:精选资料_mac十大最好看色号_自己接单的平台_seo排名优化课程

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: