0

I have System.Windows.Media.Geometry elements that each is initialized by the following properties:

Path, height and width.

The geometries are then inserted into a System.Windows.Media.GeometryGroup. But this groups bounds are zero, no matter what the geomtries inserted are. WHy is this?

Further since I am creating a GeometryGroup I would like to position the elements in the Group based on x y and z if possible such that I can create my geometry element, like this example shows.

Basicly my question is: The user adds elements together, and I want to create a new element based on these figures, such that a new path can be made for the users defined element. Lets say I have a square and a triangle. The user combines this to a house. Now I want to make the house an element for the user. For this I need the path of the element, how do I create this?

JTIM
  • 2,774
  • 1
  • 34
  • 74
  • possible duplicate of [Convert path to geometric shape](http://stackoverflow.com/questions/22989172/convert-path-to-geometric-shape) – meneses.pt Aug 08 '14 at 15:58
  • It is a bit the same but was to make it possible to divide the questions up. Thought that made sense ? – JTIM Aug 08 '14 at 18:07
  • `System.Windows.Media.Geometry` has none of the properties `Path`, `Width` or `Height`. Please be more specific about what you are asking. Adding a code sample might also be helpful. Moreover, your question is tagged `WPF` and `windows-phone-8`. That is contradictory. It can only be one of them, not both. – Clemens Aug 12 '14 at 16:22
  • I tagged both because the problem could Be solved in wpf, such that a solution could be created for windows phone. Since most questions i found in this area was in wpf and I could test them on the phone I am aware that it is two different areas, although they overlap. Again you are correct that they do not posses the path specification but it can be given to it by parsing it as an xaml string http://stackoverflow.com/questions/22989172/convert-path-to-geometric-shape I did not include code since I have not been able to specify the position, but it should be possible when using geometrygroup. – JTIM Aug 12 '14 at 18:15

2 Answers2

1

Here is an xaml definition i pulled off the example you linked, so we have something concrete to talk about.

<Path Stroke="Black" StrokeThickness="3" Fill="Blue" >
    <Path.Data>
        <GeometryGroup >
            <LineGeometry StartPoint="20,200" EndPoint="300,200" />
            <EllipseGeometry Center="80,150" RadiusX="50" RadiusY="50" />
            <RectangleGeometry Rect="80,167 150 30"/>
        </GeometryGroup>
    </Path.Data>
</Path>

Where is the origin of this path? It does not define one. If you remove the LineGeometry from this group and its now in a group of its own, does it still begin at 20,200? Yes.

So lets make up some arbitrary rules.

The first geometry in any group is always a LineGeometry of zero length, that is, its StartPoint and EndPoint are the same. This geometry defines the origin. This LineGeometry is never removed from a group; only shapes below it are added and removed.

When you remove a geometry from a group, you subtract any positional data by the origin.
When you add a geometry to a group, you add to any positional data by the origin.

Example, you want to move the EllipseGeometry from Path1 to Path2...

// Path1
<Path Stroke="Black" StrokeThickness="3" Fill="Blue" >
    <Path.Data>
        <GeometryGroup >
            <LineGeometry StartPoint="100,0" EndPoint="100,0" />
            <EllipseGeometry Center="100,0" RadiusX="50" RadiusY="50" />
        </GeometryGroup>
    </Path.Data>
</Path>

// Path2
<Path Stroke="Black" StrokeThickness="3" Fill="Blue" >
    <Path.Data>
        <GeometryGroup >
            <LineGeometry StartPoint="100,100" EndPoint="100,100" />
        </GeometryGroup>
    </Path.Data>
</Path>

Remove EllipseGeometry from Path1...
EllipseGeometry.Center = 100,0 - 100,0 = 0,0

Add EllipseGeometry to Path2...
EllipseGeometry.Center = 0,0 + 100,100 = 100,100

Tadda, the ellipse which previously rendered at the origin of one path now renders at the origin of another path.

James
  • 1,973
  • 1
  • 18
  • 32
  • Thank you for your description it made some of it more clear. I am doing the code in c# instead of xaml. I have been able to create geometrygroups of standard element such as Linegeometry, ellipsegeometry and rectanglegeometry. However when I introduce geometries created by paths I am not able to render them or position them, when using geometry paths. So my question is how do you do the same you did but with path elements and not standard geometries. I have been able to put a path into a geometry, but it is not rendered properly. – JTIM Aug 14 '14 at 06:40
  • When you say "geometries created by paths" what is the actual concrete type of that geometry? You will need to cast it to its concrete type and then perform addition/sutraction on the appropriate fields of it representing its position. – James Aug 14 '14 at 19:12
1

by using transformation, more specifically composittransform you can add that to many different geometries. Such that position and everything else can be set as you want. But you might want to look into the pathgeometry instead.

JonasN89
  • 1,386
  • 2
  • 11
  • 23