private void TransformPoints(Glyph fromGlyph, VectorTransform transform) { if (HoleSorted && !fromGlyph.WasHoleSorted) { // We must sort all components: fromGlyph.HoleSort(); } VectorPoint current = fromGlyph.FirstPathNode; while (current != null) { // Create a new one: VectorPoint newPoint = current.Copy(); // Apply transformed pos: newPoint.Transform(transform); // Add it: AddPathNode(newPoint); current = current.Next; if (current == null) { // Last one - ensure it's closed: newPoint.IsClose = true; } } }
/// <summary>Draws the outline of path you created, and doesn't reset the path, using the stroke style.</summary> public void stroke() { if (Clip_) { // Clip the path first. Clip_ = false; // Clip with a 50px safety zone on all sides for strokes. Path.Clip(-50f, -50f, ImageData.Width + 50f, ImageData.Height + 50f); } // For each line.. DynamicTexture img = ImageData_; VectorPoint node = Path.FirstPathNode; // For each one.. while (node != null) { // Render it as a line (if it has one; checks internally): if (node.HasLine) { // Render the line from the next nodes point of view: node.RenderLine(this); } // Hop to the next one: node = node.Next; } // Request a paint: img.RequestPaint(); }
public double DistanceFrom(VectorPoint vp) { return(Math.Sqrt( Math.Pow(X - vp.X, 2) + Math.Pow(Y - vp.Y, 2) + Math.Pow(Z - vp.Z, 2)) / 1000d); }
// each of these deals with drawing either object private void drawLine(SKCanvas canvas, VectorLine line, bool outline) { // make the paint style SKPaint paint = new SKPaint(); paint.StrokeWidth = line.strokeThickness; paint.Color = SKColor.Parse(line.colour); VectorPoint prevPoint = new VectorPoint(0, 0); float x = line.position.x; float y = line.position.y; //loop through and smaller line // the smaller lines is previous point and then this point foreach (VectorPoint point in line.points) { canvas.DrawLine(convertXCoord(prevPoint.x + x), convertYCoord(prevPoint.y + y), convertXCoord(point.x + x), convertYCoord(point.y + y), paint); prevPoint = point; } // if applicable, calculate the bounding box and draw it if (outline) { float lx = convertXCoord(line.minX + line.position.x) - outlinePadding; float ly = convertYCoord(line.minY + line.position.y) - outlinePadding; float w = (Math.Abs(line.maxX) + Math.Abs(line.minX)) * scale + (outlinePadding * 2); float h = (Math.Abs(line.maxY) + Math.Abs(line.minY)) * scale + (outlinePadding * 2); canvas.DrawRect(lx, ly, w, h, outlinePaint); } }
private static void Main() { var client = new ServiceDistanceCalculatorClient(); var start = new VectorPoint { X = 5, Y = 5 }; var end = new VectorPoint { X = 10, Y = 10 }; var result = client.CalculateDistance(start, end); Console.WriteLine(result); }
public PiranhaPlant(Level l, Point position, Sprite sprite) : base(l, position, sprite, NullSprite.Instance) { BoundingBox = new Bounds(this, Color.Red, new Vector2(2, 2), new Vector2(12, 22)); HasGravity = false; this.Position -= new Point(-7, 8); this.PrevPosition = this.Position; this.Sprite.Layer = 0f; originalposition = position; this.Velocity = Vector2.Zero; }
/// <summary> /// Used to generate the smooth curve control points. /// </summary> public static void Reflect(VectorPath path, out float cx, out float cy) { // - Take the last control point from the previous curve // - Reflect it around the start point // - Result is 'my' first control point VectorPoint node = path.LatestPathNode; if (node == null) { cx = 0f; cy = 0f; return; } float lastControlX = 0f; float lastControlY = 0f; float reflectAroundX = node.X; float reflectAroundY = node.Y; // Try as a curve: CurveLinePoint clp = node as CurveLinePoint; if (clp == null) { // Try quad point instead: QuadLinePoint qlp = node as QuadLinePoint; if (qlp == null) { cx = path.LatestPathNode.X; cy = path.LatestPathNode.Y; return; } lastControlX = qlp.Control1X; lastControlY = qlp.Control1Y; } else { lastControlX = clp.Control2X; lastControlY = clp.Control2Y; } // Reflect lastControl around reflectAround: // reflectAround-(COORD-reflectAround) cx = 2f * reflectAroundX - lastControlX; cy = 2f * reflectAroundY - lastControlY; }
// this finds the object in which the point is lying in, uses the bounding box idea to basically calculate if it is in between the box // this could do with some tweaking but its big and scary // the general idea is to loop through every object and check if it lies in between the box, if not ignore // the concept is repeated for all the object types, but they all do the same thing public void findSelected(VectorPoint seletedPoint) { foreach (KeyValuePair <string, VectorObject> obj in objects) { if (obj.Value is VectorBox) { VectorBox box = obj.Value as VectorBox; if (box.position.x <= seletedPoint.x && box.position.x + box.size.x >= seletedPoint.x) { if (box.position.y <= seletedPoint.y && box.position.y + box.size.y >= seletedPoint.y) { outlinedObject = obj.Key; } } } else if (obj.Value is VectorLine) { VectorLine line = obj.Value as VectorLine; if (line.minX + line.position.x <= seletedPoint.x && line.maxX + line.position.x >= seletedPoint.x) { if (line.minY + line.position.y <= seletedPoint.y && line.maxY + line.position.y >= seletedPoint.y) { outlinedObject = obj.Key; } } } else if (obj.Value is VectorEllipse) { VectorEllipse ellipse = obj.Value as VectorEllipse; if (ellipse.position.x - ellipse.radii.x <= seletedPoint.x && ellipse.position.x + ellipse.radii.x >= seletedPoint.x) { if (ellipse.position.y - ellipse.radii.y <= seletedPoint.y && ellipse.position.y + ellipse.radii.y >= seletedPoint.y) { outlinedObject = obj.Key; } } } else if (obj.Value is VectorText) { VectorText text = obj.Value as VectorText; if (text.position.x <= seletedPoint.x && text.position.x + text.width >= seletedPoint.x) { if (text.position.y - text.fontSize <= seletedPoint.y && text.position.y >= seletedPoint.y) { outlinedObject = obj.Key; } } } } }
private void TransformPoints(Glyph fromGlyph, VectorTransform transform) { VectorPoint current = fromGlyph.FirstPathNode; while (current != null) { // Create a new one: VectorPoint newPoint = current.Copy(); // Apply transformed pos: newPoint.Transform(transform); // Add it: AddPathNode(newPoint); current = current.Next; } }
/// <summary>Is the specified point in (not on) the current path?</summary> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> public bool isPointInPath(float x, float y) { VectorPoint node = Path.FirstPathNode; // For each one.. while (node != null) { // Got a match? if (node.X == x && node.Y == y) { return(true); } // Hop to the next one: node = node.Next; } return(false); }
/// <summary>Draws the outline of path you created, and doesn't reset the path, using the stroke style.</summary> public void stroke() { // For each line.. VectorPoint node = Path.FirstPathNode; // For each one.. while (node != null) { // Render it as a line (if it has one; checks internally): if (node.HasLine) { // Render the line from the next nodes point of view: node.RenderLine(this); } // Hop to the next one: node = node.Next; } }
// Initialize VectorField and create particles at set interval void Start() { VectorField = new VectorPoint[NUM_BINS_Y][]; int count = 0; for (int i = 0; i < NUM_BINS_Y; i++) { VectorField[i] = new VectorPoint[NUM_BINS_X]; for (int z = 0; z < NUM_BINS_X; z++) { float xCoord = (GetBinW() * i) - (FIELD_WIDTH / 2); float zCoord = (GetBinH() * z) - (FIELD_HEIGHT / 2); VectorPoint point = Instantiate(VP, new Vector3(xCoord, 0, zCoord), StartPos.rotation); point.IndexX = i; point.IndexY = z; if (count % 4 == 0) { point.particleCreator = true; } if (count % 6 == 0) { point.longTermCreator = true; } count++; VectorField[i][z] = point; if (DebugOn) { VectorField[i][z].GetComponent <MeshRenderer>().enabled = true; } else { VectorField[i][z].GetComponent <MeshRenderer>().enabled = false; } } } //LoadRiver(); // create new particles every x seconds //InvokeRepeating("CreateParticles", 0f, pCreationInterval); }
/// <summary>Draws the outline of path you created, and doesn't reset the path, using the stroke style.</summary> public void stroke() { // For each line.. DynamicTexture img = ImageData_; VectorPoint node = Path.FirstPathNode; // For each one.. while (node != null) { // Render it as a line (if it has one; checks internally): if (node.HasLine) { // Render the line from the next nodes point of view: node.RenderLine(this); } // Hop to the next one: node = node.Next; } // Request a paint: img.RequestPaint(); }
public void CalcPartialOffSet(int toCalc) { _toCalc = toCalc; toCalc = (int)Math.Ceiling((double)toCalc / (double)Constants.VECTOR_LENGTH); double offset = 0; int index = 0; for (int i = 0; i < toCalc; i++) { VectorPoint Coord = EInfo.Goal[i]; Vector <double> FunctionResult = GetFunctionResult(Coord.X); double[] partResult = Tools.GetPartOfVectorResult(FunctionResult, Coord.Count); offset += CalcOffset(partResult, Coord.Y, Coord.Count); if (!Tools.IsANumber(offset)) { this.OffSet = double.NaN; return; } Array.Copy(partResult, 0, Results, index, partResult.Length); index += Coord.Count; } this.OffSet = offset; }
/** * Constructs a new RectPoint with the same coordinates as the given VectorPoint. */ private RectPoint(VectorPoint vector) { x = vector.X; y = vector.Y; }
/** * Constructs a new DiamondPoint with the same coordinates as the given VectorPoint. */ private DiamondPoint(VectorPoint vector) { x = vector.X; y = vector.Y; }
/** * Constructs a new FlatHexPoint with the same coordinates as the given VectorPoint. */ private FlatHexPoint(VectorPoint vector) { x = vector.X; y = vector.Y; }
/// <summary>Adds an arc between the given points using the pen point as a control point. /// Note that nothing will be seen until you call a fill or stroke method.</summary> public void arcTo(float x1, float y1, float x2, float y2, float radius) { VectorPoint previous = Path.LatestPathNode; float x0; float y0; if (previous == null) { x0 = 0f; y0 = 0f; } else { x0 = previous.X; y0 = previous.Y; } // How this works: // Line from 0->1. // Another from 2->1, thus forming a 'triangle'. // Slide a circle of the given radius in this triangle such that it's // as far in as it will go whilst just touching both lines. // Draw an arc from the two intersection points of the circle with the triangle. // What we need to find: // - Circle center. // - The start and end angles. Always clockwise. // Create the new arc node: ArcLinePoint arcNode = new ArcLinePoint(x2, y2); // Apply the radius: arcNode.Radius = radius; // First find the angle of 1 from +ve x. This is just an Atan2 call: float angleLine2 = (float)Math.Atan2(y0 - y1, x0 - x1); // Find the angle of 2 relative to 1. // As we know the angle of 1 relative to +ve x, we can find 1 relative to +ve x // and then grab the difference. float rotateThrough = (float)Math.Atan2(y2 - y1, x2 - x1) - angleLine2; // Find half of the angle: float halfAngle = rotateThrough / 2f; // What's the distance of point 1 to the circle center? float distanceToCenter = radius / (float)Math.Cos(halfAngle); // Resolve the x coordinate of the center: arcNode.CircleCenterX = (distanceToCenter * (float)Math.Cos(halfAngle - angleLine2)) + x1; // Resolve the y coordinate of the center: arcNode.CircleCenterY = (distanceToCenter * (float)Math.Sin(halfAngle - angleLine2)) + y1; // Apply the angles: arcNode.StartAngle = -rotateThrough - angleLine2; arcNode.EndAngle = arcNode.StartAngle - rotateThrough; float arcStartNodeX; float arcStartNodeY; arcNode.SampleAt(0f, out arcStartNodeX, out arcStartNodeY); float arcEndNodeX; float arcEndNodeY; arcNode.SampleAt(1f, out arcEndNodeX, out arcEndNodeY); if (Path.FirstPathNode == null) { // This occurs if the arc is the first thing we draw. No line is drawn to it. Path.MoveTo(arcStartNodeX, arcStartNodeY); } else if (x0 != arcStartNodeX || y0 != arcStartNodeY) { Path.LineTo(arcStartNodeX, arcStartNodeY); } // Apply the node location: arcNode.X = arcEndNodeX; arcNode.Y = arcEndNodeY; // Add the other end: Path.AddPathNode(arcNode); }
public static Vector2 HadamardMul(this Vector2 thisVector, VectorPoint otherVector) { return new Vector2(thisVector.x * otherVector.X, thisVector.y * otherVector.Y); }
/// <summary>Creates an arc around the given circle center. Note that nothing will /// be seen until you call a fill or stroke method.</summary> public void arc(float centerX, float centerY, float radius, float sAngle, float eAngle, bool counterClockwise) { VectorPoint previous = Path.LatestPathNode; float x0; float y0; if (previous == null) { x0 = 0f; y0 = 0f; } else { x0 = previous.X; y0 = previous.Y; } // Clockwise eAngle > sAngle; counter clockwise otherwise. if (eAngle > sAngle) { if (counterClockwise) { // Get them both in range: eAngle = eAngle % (Mathf.PI * 2f); sAngle = sAngle % (Mathf.PI * 2f); // Reduce eAngle by a full rotation so it's smaller: eAngle -= (Mathf.PI * 2f); } } else if (!counterClockwise) { // Get them both in range: eAngle = eAngle % (Mathf.PI * 2f); sAngle = sAngle % (Mathf.PI * 2f); // Reduce sAngle by a full rotation so it's smaller: sAngle -= (Mathf.PI * 2f); } // First, figure out where the actual start is. // It's radius units to the right of center, then rotated through radius around center. // Thus we have a triangle with hyp length of 'radius' and an angle of sAngle: float startX = radius * (float)Math.Cos(sAngle); float startY = radius * (float)Math.Sin(sAngle); // Now find the end point, using exactly the same method: float endX = radius * (float)Math.Cos(eAngle); float endY = radius * (float)Math.Sin(eAngle); // We now have an arc from the current position to endX/endY. // The start and exit node angles are usefully just offset from the given ones. // This is because an sAngle of zero should create an arc which starts travelling downwards. // (Or upwards if it's counter clockwise): // Where does the arc start from? float arcStartX = centerX + startX; float arcStartY = centerY + startY; if (Path.FirstPathNode == null) { // This occurs if the arc is the first thing we draw. No line is drawn to it. Path.MoveTo(arcStartX, arcStartY); } else if (arcStartX != x0 || arcStartY != y0) { // Draw a line to this point: Path.LineTo(arcStartX, arcStartY); } // Create the new arc node: ArcLinePoint arcNode = new ArcLinePoint(centerX + endX, centerY + endY); // Apply the radius: arcNode.Radius = radius; // Apply the angles: arcNode.StartAngle = sAngle; arcNode.EndAngle = eAngle; // Apply the center: arcNode.CircleCenterX = centerX; arcNode.CircleCenterY = centerY; // Add the other end: Path.AddPathNode(arcNode); }
public static Vector2 HadamardDiv(this Vector2 thisVector, VectorPoint otherVector) { return new Vector2(thisVector.x / otherVector.X, thisVector.y / otherVector.Y); }
/// <summary>Generates a stroke now using these settings. /// Note that the given path may be modified.</summary> public void Generate(VectorPath path) { // First, simplify the path (eliminates loops etc): path.SimplifyCurve(); // Always recalculate bounds: path.RecalculateBounds(); // For each line, we can now generate the two // offset curves and sample them individually. float halfWidth = Width / 2f; // Tidy values: OuterLength = 0f; InnerLength = 0f; InnerCount = 0; OuterCount = 0; float nx; float ny; // Are we using linejoin? bool lineJoinActive = (LineJoinMode & StrokeLineMode.JoinActive) != 0; VectorPoint current = path.FirstPathNode; while (current != null) { VectorLine line = current as VectorLine; // Set the current line: CurrentLine = line; if (line == null) { // Move to. Complete the previous contour: CompleteContour((current as MoveToPoint).ClosePoint != null); // Go to next: current = current.Next; continue; } if (lineJoinActive) { // Get start normal: line.StartNormal(out nx, out ny); StartNormal = new Vector2(nx, ny); } // First of line: bool applyLineJoin = AtLeastOneLine && lineJoinActive; if (applyLineJoin) { // Compare StartNormal with EndNormal and figure out if we're adding extra verts to // inner or outer. // Original point: OriginalLinePoint = new Vector2( current.Previous.X, current.Previous.Y ); // Get the angle between them: float angle = (float)System.Math.Atan2( StartNormal.x * EndNormal.y - StartNormal.y * EndNormal.x, StartNormal.x * EndNormal.x + StartNormal.y * EndNormal.y ); // If we're in miter mode, find the miter length. // If it exceeds the miter limit, set angle to 0 so we just end up with a bevel. if (LineJoinMode == StrokeLineMode.Miter) { // Compute the miter length now: float miterRatio = 1f / (float)System.Math.Sin(angle / 2f); if (miterRatio > MiterLimit || -miterRatio > MiterLimit) { // Out of range! angle = 0f; } else { // Set the miter length now: MiterLength = miterRatio * halfWidth; } } LineAngle = angle; // Very close to zero -> Do nothing (this is ~5 degrees): if (angle > 0.08f) { // Apply join to outer: ApplyLineJoinNow = true; applyLineJoin = false; } else if (angle > -0.08f) { applyLineJoin = false; } // Else apply join to inner (by leaving applyLineJoin true). } // Change to outer curve: Outer = true; // Extrude and sample it: current.ExtrudeAndSample(path, halfWidth, this); // Increase length so far: OuterLength += line.Length; // Change to inner curve: Outer = false; if (applyLineJoin) { // Apply join to inner: ApplyLineJoinNow = true; } // Extrude and sample it: current.ExtrudeAndSample(path, -halfWidth, this); // Increase length so far: InnerLength += line.Length; if (lineJoinActive) { // Get end normal: line.EndNormal(out nx, out ny); EndNormal = new Vector2(nx, ny); } // We've seen at least one line: AtLeastOneLine = true; // Next: current = current.Next; } // Complete the final contour: CompleteContour(path.LatestPathNode.IsClose); }
/// <summary>Recalculates the normals of this path. Not required unless you're using SDF.</summary> public override void RecalculateNormals() { if (FirstPathNode == null) { return; } VectorPoint current = FirstPathNode; while (current != null) { // Recalc curve normals: current.RecalculateCurveNormals(); // Hop to the next one: current = current.Next; } VectorPoint moveTo = null; current = FirstPathNode; while (current != null) { // Recalc curve normals: //4. For each point, query the lines on either side for their start/end normals and avg them. float afterNX; float afterNY; float beforeNX; float beforeNY; bool updateMoveTo = false; if (!current.HasLine) { // Current is a MoveTo: moveTo = current; // Hop to the next one: current = current.Next; continue; } else if (current.IsClose || current.Next == null) { // This point is followed by a MoveTo. if (moveTo == null) { // Hop to the next one: current = current.Next; continue; } // Pair with moveTo: current.EndNormal(out beforeNX, out beforeNY); moveTo.Next.StartNormal(out afterNX, out afterNY); updateMoveTo = true; } else { // Line from current->previous and line from current->Next. current.EndNormal(out beforeNX, out beforeNY); current.Next.StartNormal(out afterNX, out afterNY); } float nX = (afterNX + beforeNX) / 2f; float nY = (afterNY + beforeNY) / 2f; // Normalise: float length = (float)Math.Sqrt((nX * nX) + (nY * nY)); current.NormalX = nX / length; current.NormalY = nY / length; if (updateMoveTo) { moveTo.NormalX = current.NormalX; moveTo.NormalY = current.NormalY; // Use it up: moveTo = null; } // Hop to the next one: current = current.Next; } }
/** * Constructs a new PointyHexPoint with the same coordinates as the given VectorPoint. */ private PointyHexPoint(VectorPoint vector) { x = vector.X; y = vector.Y; }
public static Screen Unconvert(string path) { // creates an xml doc and loads it XmlDocument doc = new XmlDocument(); Screen screen = new Screen(); doc.Load(path); if (doc.HasChildNodes) // ensures we have a screen element { // gets the stuff from the screen node XmlElement xml = (XmlElement)doc.ChildNodes[0]; screen.current_colour = xml.GetAttributeNode("colour").InnerXml; screen.translateX = float.Parse(xml.GetAttributeNode("translateX").InnerXml); screen.translateY = float.Parse(xml.GetAttributeNode("translateY").InnerXml); screen.outlinedObject = xml.GetAttributeNode("outlined").InnerXml; screen.stroke_thickness = float.Parse(xml.GetAttributeNode("stroke").InnerXml); screen.font_size = float.Parse(xml.GetAttributeNode("fontSize").InnerXml); screen.current_fill_in = bool.Parse(xml.GetAttributeNode("currentFillIn").InnerXml); if (xml.HasChildNodes) { // loops through all the objects and creates objects then adds them to the screen class, mostly self explanatory and repetitive code for (int i = 0; i < xml.ChildNodes.Count; i++) { XmlElement child = (XmlElement)xml.ChildNodes[i]; string[] xandy = child.GetAttributeNode("position").InnerXml.Split(','); float x = float.Parse(xandy[0]); float y = float.Parse(xandy[1]); VectorPoint position = new VectorPoint(x, y); string colour = child.GetAttributeNode("colour").InnerXml; string uid = child.GetAttributeNode("uid").InnerXml; switch (child.Name) { case "line": VectorLine line = new VectorLine(position); line.minX = float.Parse(child.GetAttributeNode("minX").InnerXml); line.minY = float.Parse(child.GetAttributeNode("minY").InnerXml); line.maxX = float.Parse(child.GetAttributeNode("maxX").InnerXml); line.maxY = float.Parse(child.GetAttributeNode("maxY").InnerXml); line.colour = colour; line.strokeThickness = float.Parse(child.GetAttributeNode("stroke").InnerXml); for (int u = 0; u < child.ChildNodes.Count; u++) { XmlElement point = (XmlElement)child.ChildNodes[u]; xandy = point.GetAttributeNode("position").InnerXml.Split(','); x = float.Parse(xandy[0]); y = float.Parse(xandy[1]); line.addPoint(new VectorPoint(x, y)); } screen.addObject(uid, line); break; case "box": xandy = child.GetAttributeNode("size").InnerXml.Split(','); x = float.Parse(xandy[0]); y = float.Parse(xandy[1]); VectorPoint size = new VectorPoint(x, y); bool fillin = bool.Parse(child.GetAttributeNode("fillin").InnerXml); float stroke = float.Parse(child.GetAttributeNode("stroke").InnerXml); VectorBox box = new VectorBox(position, size, fillin); box.strokeThickness = stroke; screen.addObject(uid, box); break; case "ellipse": xandy = child.GetAttributeNode("radii").InnerXml.Split(','); x = float.Parse(xandy[0]); y = float.Parse(xandy[1]); VectorPoint radii = new VectorPoint(x, y); fillin = bool.Parse(child.GetAttributeNode("fillin").InnerXml); stroke = float.Parse(child.GetAttributeNode("stroke").InnerXml); VectorEllipse ellipse = new VectorEllipse(position, radii, fillin); ellipse.strokeThickness = stroke; screen.addObject(uid, ellipse); break; case "text": string text = child.GetAttributeNode("text").InnerXml; float fontsize = float.Parse(child.GetAttributeNode("fontSize").InnerXml); VectorText obj = new VectorText(position, text); obj.colour = colour; obj.fontSize = fontsize; screen.addObject(uid, obj); break; } } } } return(screen); }
public WalkingState(Spiny spiny, Sprite walkingSprite, VectorPoint walkSpeed) : base(spiny) { this.walkingSprite = walkingSprite; this.walkSpeed = walkSpeed; }