/// <summary> /// Obtains a list of vertexes that represent the polyline approximating the curve segments as necessary. /// </summary> /// <param name="bulgePrecision">Curve segments precision (a value of zero means that no approximation will be made).</param> /// <param name="weldThreshold">Tolerance to consider if two new generated vertexes are equal.</param> /// <param name="bulgeThreshold">Minimun distance from which approximate curved segments of the polyline.</param> /// <returns>The return vertexes are expresed in object coordinate system.</returns> public List<Vector2f> PoligonalVertexes(int bulgePrecision, float weldThreshold, float bulgeThreshold) { List<Vector2f> ocsVertexes = new List<Vector2f>(); int index = 0; foreach (PolylineVertex vertex in this.Vertexes) { float bulge = vertex.Bulge; Vector2f p1; Vector2f p2; if (index == this.Vertexes.Count - 1) { p1 = new Vector2f(vertex.Location.X, vertex.Location.Y); p2 = new Vector2f(this.vertexes[0].Location.X, this.vertexes[0].Location.Y); } else { p1 = new Vector2f(vertex.Location.X, vertex.Location.Y); p2 = new Vector2f(this.vertexes[index + 1].Location.X, this.vertexes[index + 1].Location.Y); } if (!p1.Equals(p2, weldThreshold)) { if (bulge == 0 || bulgePrecision == 0) { ocsVertexes.Add(p1); } else { float c = Vector2f.Distance(p1, p2); if (c >= bulgeThreshold) { float s = (c / 2) * Math.Abs(bulge); float r = ((c / 2) * (c / 2) + s * s) / (2 * s); float theta = (float)(4 * Math.Atan(Math.Abs(bulge))); float gamma = (float)((Math.PI - theta) / 2); float phi; if (bulge > 0) { phi = Vector2f.AngleBetween(Vector2f.UnitX, p2 - p1) + gamma; } else { phi = Vector2f.AngleBetween(Vector2f.UnitX, p2 - p1) - gamma; } Vector2f center = new Vector2f((float)(p1.X + r * Math.Cos(phi)), (float)(p1.Y + r * Math.Sin(phi))); Vector2f a1 = p1 - center; float angle = 4 * ((float)(Math.Atan(bulge))) / (bulgePrecision + 1); ocsVertexes.Add(p1); for (int i = 1; i <= bulgePrecision; i++) { Vector2f curvePoint = new Vector2f(); Vector2f prevCurvePoint = new Vector2f(this.vertexes[this.vertexes.Count - 1].Location.X, this.vertexes[this.vertexes.Count - 1].Location.Y); curvePoint.X = center.X + (float)(Math.Cos(i * angle) * a1.X - Math.Sin(i * angle) * a1.Y); curvePoint.Y = center.Y + (float)(Math.Sin(i * angle) * a1.X + Math.Cos(i * angle) * a1.Y); if (!curvePoint.Equals(prevCurvePoint, weldThreshold) && !curvePoint.Equals(p2, weldThreshold)) { ocsVertexes.Add(curvePoint); } } } else { ocsVertexes.Add(p1); } } } index++; } return ocsVertexes; }
/// <summary> /// Converts the arc in a list of vertexes. /// </summary> /// <param name="precision">Number of vertexes generated.</param> /// <param name="weldThreshold">Tolerance to consider if two new generated vertexes are equal.</param> /// <returns>A list vertexes that represents the arc expresed in object coordinate system.</returns> public List<Vector2f> PoligonalVertexes(int precision, float weldThreshold) { if (precision < 2) throw new ArgumentOutOfRangeException("precision", precision, "The arc precision must be greater or equal to two"); List<Vector2f> ocsVertexes = new List<Vector2f>(); float start = (float)(this.anguloInicio * MathHelper.DegToRad); float end = (float)(this.anguloFin * MathHelper.DegToRad); if (2 * this.radio >= weldThreshold) { float angulo = (end - start) / precision; Vector2f prevPoint; Vector2f firstPoint; float sine = (float)(this.radio * Math.Sin(start)); float cosine = (float)(this.radio * Math.Cos(start)); firstPoint = new Vector2f(cosine + this.centro.X, sine + this.centro.Y); ocsVertexes.Add(firstPoint); prevPoint = firstPoint; for (int i = 1; i <= precision; i++) { sine = (float)(this.radio * Math.Sin(start + angulo * i)); cosine = (float)(this.radio * Math.Cos(start + angulo * i)); Vector2f point = new Vector2f(cosine + this.centro.X, sine + this.centro.Y); if (!point.Equals(prevPoint, weldThreshold) && !point.Equals(firstPoint, weldThreshold)) { ocsVertexes.Add(point); prevPoint = point; } } } return ocsVertexes; }
/// <summary> /// Convierte el circulo en una lista de vertices. /// </summary> /// <param name="precision">Numero de vertices generados.</param> /// <param name="tolerancia">Tolerancia a considerar para comparar si dos nuevos vertices son iguales.</param> /// <returns>Una lista de verices que representa el circulo expresado en coordenadas.</returns> public List<Vector2f> PoligonalVertices(int precision, float tolerancia) { if (precision < 3) throw new ArgumentOutOfRangeException("precision", precision, "La precision del circulo debe ser mayor o igual a tres"); List<Vector2f> ocsVertices = new List<Vector2f>(); if (2 * this.radio >= tolerancia) { float angulo = (float)(MathHelper.TwoPI / precision); Vector2f antPunto; Vector2f primerPunto; float seno = (float)(this.radio * Math.Sin(MathHelper.HalfPI * 0.5)); float coseno = (float)(this.radio * Math.Cos(MathHelper.HalfPI * 0.5)); primerPunto = new Vector2f(coseno + this.centro.X, seno + this.centro.Y); ocsVertices.Add(primerPunto); antPunto = primerPunto; for (int i = 1; i < precision; i++) { seno = (float)(this.radio * Math.Sin(MathHelper.HalfPI + angulo * i)); coseno = (float)(this.radio * Math.Cos(MathHelper.HalfPI + angulo * i)); Vector2f punto = new Vector2f(coseno + this.centro.X, seno + this.centro.Y); if (!punto.Equals(antPunto, tolerancia) && !punto.Equals(primerPunto, tolerancia)) { ocsVertices.Add(punto); antPunto = punto; } } } return ocsVertices; }