Line and Point Classes

Sep 24, 2008 at 7:44 PM
For what its worth:

I added line and point classes to my app to display line and point features; they work fine in my application.  The point class (called ellipseshape) corrects the problem I was having (location shift) when adding point either by clicking on screen or through reading existing coordinates.  Someone can look them over and improve on them.  Notes: 1) The line class allows multiple distinct line segments to be added but that control is done inside Page.xaml.cs.  Originally I tried to do this within the line class using PathFigureCollection but it has a bug in it (check MS Silverlight forums).  For now, the class looks remarkably like the existing polygon class.  2)not sure the Intersects functions are correct.

ellipseshape - POINT CLASS

using

System;

 

using

System.Net;

 

using

System.Windows;

 

using

System.Windows.Controls;

 

using

System.Windows.Documents;

 

using

System.Windows.Ink;

 

using

System.Windows.Input;

 

using

System.Windows.Media;

 

using

System.Windows.Media.Animation;

 

using

System.Windows.Shapes;

 

namespace

DeepEarth

 

{

 

public class EllipseShape : Shape

 

{

 

const double size = 3.5;

 

 

private Point point;

 

 

public Point Point

 

{

 

get { return point; }

 

 

set

 

{

 

if (point != value)

 

{

point =

value;

 

Refresh();

}

}

}

 

public override bool Intersects(Rect bounds)

 

{

 

var intersect = new Rect(bounds.X, bounds.Y, bounds.Width, bounds.Height);

 

 

return intersect.Contains(point);

 

 

//intersect.Intersect(extent);

 

 

//return (intersect.Width > 0 || intersect.Height > 0);

 

}

 

public override UIElement CreateElement(IProjection projection)

 

{

 

Path path = CreatePointShape();

 

 

return path;

 

}

 

public Path CreatePointShape()

 

{

 

var path = new Path

 

{

Data = CreateGeometry(),

 

//Fill = new SolidColorBrush(Colors.Yellow),

 

Fill =

new SolidColorBrush(setFillColor),

 

Stroke =

new SolidColorBrush(Colors.Black),

 

StrokeThickness = 0.5

};

 

return path;

 

}

 

private EllipseGeometry CreateGeometry()

 

{

 

EllipseGeometry pEGeom = new EllipseGeometry();

 

 

//var pEGeom = new EllipseGeometry();

 

 

Point pixel = Map.GetProjectionToDisplay(Point);

 

pEGeom.Center = pixel;

pEGeom.RadiusX = size;

pEGeom.RadiusY = size;

 

return pEGeom;

 

}

 

public override void UpdateElement(UIElement element, IProjection projection)

 

{

 

Path path = element as Path;

 

 

if (path != null) path.Data = CreateGeometry();

 

}

}

}

***********************************************

***********************************************
lineshape  - LINE CLASS


using
System.Collections.Generic;

 

 

 

 

using

System.Windows;

 

 

 

 

using

System.Windows.Media;

 

 

 

 

using

System.Windows.Shapes;

 

 

 

 

namespace

DeepEarth

 

{

 

public class LineShape : Shape

 

 

 

 

{

 

 

private List<Point> points;

 

 

private Rect extent { get; set; }

 

 

public List<Point> Points

 

{

 

get { return points; }

 

 

set

 

 

 

 

{

points =

value;

 

 

double minx = double.MaxValue;

 

 

double miny = double.MaxValue;

 

 

double maxx = double.MinValue;

 

 

double maxy = double.MinValue;

 

 

foreach (Point pt in points)

 

{

 

if (pt.X < minx) minx = pt.X;

 

 

if (pt.Y < miny) miny = pt.Y;

 

 

if (pt.X > maxx) maxx = pt.X;

 

 

if (pt.Y > maxy) maxy = pt.Y;

 

}

extent =

new Rect(new Point(minx, miny), new Point(maxx, maxy));

 

}

}

 

public override bool Intersects(Rect bounds)

 

{

 

var intersect = new Rect(bounds.X, bounds.Y, bounds.Width, bounds.Height);

 

intersect.Intersect(extent);

 

return (intersect.Width > 0 || intersect.Height > 0);

 

 

//return false;

 

 

 

 

}

 

public override UIElement CreateElement(IProjection projection)

 

{

 

Path path = CreateLine();

 

 

return path;

 

}

 

private Path CreateLine()

 

{

 

var path = new Path

 

 

 

 

{

Data = CreateGeometry(),

StrokeThickness = 2,

Stroke =

new SolidColorBrush(setFillColor)

 

};

 

return path;

 

}

 

 

private PathGeometry CreateGeometry()

 

{

 

var geom = new PathGeometry();

 

 

var pf = new PathFigure();

 

 

int idx = 0;

 

 

foreach (Point pt in Points)

 

{

 

Point pixel = Map.GetProjectionToDisplay(pt);

 

 

if (idx == 0)

 

{

pf.StartPoint = pixel;

}

 

else

 

 

 

 

{

 

var ls = new LineSegment { Point = pixel };

 

pf.Segments.Add(ls);

}

idx++;

}

 

geom.Figures.Add(pf);

 

return geom;

 

}

 

public override void UpdateElement(UIElement element, IProjection projection)

 

{

 

Path path = element as Path;

 

 

if (path != null) path.Data = CreateGeometry();

 

}

}

}