//************************************************************************************** void addCoordinate(DimensionType dimension, out Point pt, bool negative) { double?[] values = new double?[] { null, null, null, null }; for (int i = 0; i < dimension.GetDimensionValue(); i++) { double value = m_random.NextDouble(); if (negative) { value = -value; } values[i] = value; m_arr.AddBytes(doubleToBytes(value)); this.wkt += value; this.ewkt += value; if (i < dimension.GetDimensionValue() - 1) { this.wkt += " "; this.ewkt += " "; } } //Set the validation element if (dimension == DimensionType.XYM) { pt = new Point(values[0], values[1], null, values[2]); } else { pt = new Point(values[0], values[1], values[2], values[3]); } }
/// <summary> /// Returns the distance from another point in a defined dimension. /// </summary> /// <param name="other"></param> /// <param name="dimension"></param> /// <returns></returns> public double DistanceFrom(Point other, DimensionType dimension) { //Argument validation if (other.IsEmpty || this.IsEmpty) { throw new ArgumentException("Point cannot be empty."); } if (dimension.GetDimensionValue() > other.Dimension.GetDimensionValue()) { throw new ArgumentException("Points must have an equivalent dimension."); } double xdis = Math.Pow(X.GetValueOrDefault() - other.X.GetValueOrDefault(), 2); double ydis = Math.Pow(Y.GetValueOrDefault() - other.Y.GetValueOrDefault(), 2); double zdis = 0.0d; double mdis = 0.0d; switch (dimension) { case DimensionType.XY: break; case DimensionType.XYZ: zdis = Math.Pow(Z.GetValueOrDefault() - other.Z.GetValueOrDefault(), 2); break; case DimensionType.XYM: mdis = Math.Pow(M.GetValueOrDefault() - other.M.GetValueOrDefault(), 2); break; case DimensionType.XYZM: zdis = Math.Pow(Z.GetValueOrDefault() - other.Z.GetValueOrDefault(), 2); mdis = Math.Pow(M.GetValueOrDefault() - other.M.GetValueOrDefault(), 2); break; default: break; } return(Math.Sqrt(xdis + ydis + zdis + mdis)); }
/// <summary> /// Initalize a point using an array and a dimension. /// </summary> /// <param name="components"></param> /// <param name="dimension"></param> public Point(double[] components, DimensionType dimension = DimensionType.XY) { if (components.Length < 2) { throw new ArgumentException("Components must have at least 2 values."); } if (components.Length != dimension.GetDimensionValue()) { throw new ArgumentException("Components must be the same size as the dimension value."); } X = components[0]; Y = components[1]; switch (dimension) { case DimensionType.XY: break; case DimensionType.XYZ: Z = components[2]; break; case DimensionType.XYM: M = components[2]; break; case DimensionType.XYZM: Z = components[2]; M = components[3]; break; default: break; } }
public Point ReadCoordinate(DimensionType dimension, string coord) { string[] svalues = coord.Split(' '); double[] dvalues = new double[svalues.Length]; for (int i = 0; i < svalues.Length; i++) { if (double.TryParse(svalues[i], out double value)) { dvalues[i] = value; } else { throw new Exception("Error parsing a numeric value near the index " + m_currIndex); } } //Validate the readed values if (dimension.GetDimensionValue() != dvalues.Length) { throw new Exception("Wrong number of arguments, found " + dvalues.Length + " expecting " + dimension.GetDimensionValue()); } //Point to store values Point pt = new Point(); switch (dimension) { case DimensionType.XY: //Setup the values pt.X = dvalues[0]; pt.Y = dvalues[1]; break; case DimensionType.XYZ: //Setup the values pt.X = dvalues[0]; pt.Y = dvalues[1]; pt.Z = dvalues[2]; break; case DimensionType.XYM: //Setup the values pt.X = dvalues[0]; pt.Y = dvalues[1]; pt.M = dvalues[2]; break; case DimensionType.XYZM: //Setup the values pt.X = dvalues[0]; pt.Y = dvalues[1]; pt.Z = dvalues[2]; pt.M = dvalues[3]; break; default: break; } return(pt); }