6

Is there any way to make lines between points, given a simple geometry as line style, using WPF geometries? I know it is possible to make these kind of lines:

-- -- --- --

But I want to make lines, using any simple geometry (e.g: the '^' symbol). So what I want is something like these: (the line may not necessarily be horizontal or vertical):

^^^^^^^^^^^^^^^^^    
*****************

Note: I don't want to make line with some characters. I want to do it using any arbitrary geometries (e.g: start shape, triangle, or any other geometry). In other word I want to repeat some geometries along a linear path between two points. So these simple geometries may be rotated somehow to follow the line and ...

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
  • The output I'm looking for should be of geometry types. for example: `StreamGeometry` – Hossein Narimani Rad Jan 27 '13 at 08:25
  • Have you tried using `System.Windows.Shapes.Line` with a custom brush for the `Stroke` property? – Simon Mourier Jan 30 '13 at 09:18
  • I tried the `StrokeDashArray` but it didn't solve the problem. I make a `StreamGeometry` with the line data and then create a `Path` from them. Can you explain more how it is possible to use the custom brush in this case. (code sample will be helpful) – Hossein Narimani Rad Jan 30 '13 at 10:09
  • This might be helpful for you. http://stackoverflow.com/questions/807428/wpf-how-can-i-make-a-brush-that-paints-graph-paper-like-squares – kenny Feb 01 '13 at 23:43
  • Thanks, that a good link but for general lines (with arbitrary slope) it seems the brush must be somehow rotated! otherwise it will not draw the desired shape. – Hossein Narimani Rad Feb 02 '13 at 03:25
  • 1
    To rotate the brush simply calculate the angle (in degrees) of the line You want to draw with the brush, e.g. `double r = 180 * Math.Atan2(p2.Y - p1.Y, p2.X - p1.X) / Math.PI`, and then set the `Transform` property of Your brush to `new RotateTransform(-r)` (You need to use negative value of the angle because `RotateTransform` rotates clockwise). Should You need to also have other transforms on Your brush, use `TransformGroup` instead. – Grx70 Feb 02 '13 at 09:44
  • 1
    Have you tried using Image Brush (Tile Brush)? http://msdn.microsoft.com/en-us/library/aa970682.aspx , also have a look at other brush How to at the same link. – Akash Kava Feb 02 '13 at 10:22

2 Answers2

1

If I understand correctly, you'd like to use the * or ^ or ! as a line essentially. Rather then use a normal solid, dash, dotted, and so on line you'd like to use physical characters? But you'd like those characters to become a Geometry object.

You could do something like:

// Create a line of characters.
string lineString = "^^^^^^^^^^^^^^";

// Create Formatted Text, customize accordingly.
FormattedText formatText = new FormattedText(
     lineString, CultureInfo.GetCultureInfo("en-us"),
     FlowDirection.LeftToRight,
     new Typeface("Arial"), 32, Brushes.Black);

// Set the Width and Height.
formatText.MaxTextWidth = 200;
formatText.MaxTextHeight = 100;

// You can obviously add as many customization's and outputs of your choice.

Now I understand this isn't what you want, you want the above string to act in Geometry. To accomplish that; you just need to do:

// Build Geometry object to represent text.
Geometry lineGeometry = formatText.BuildGeometry(new System.Windows.Point(0, 0));

// Tailor Geometry object that represents our item.
Geometry hGeo = formatText.BuildHighlightGeometry(new System.Windows.Point(0, 0));

Now essentially you've built a Geometry object that represents "^^^^^^^^".

Hopefully I understood correctly, I don't know if that solves your problem.

Greg
  • 11,302
  • 2
  • 48
  • 79
  • Thanks, but what if I want to draw custom line between two Points (p1, p2). Moreover I'm not going to make lines with some characters. I want to be able to make line with any custom geometry shape. Of course this geometry shape may be similar to some characters – Hossein Narimani Rad Feb 02 '13 at 03:11
  • That is what my answer is doing; turning the `formatText` intoa Geometry shape. Is that not what you meant? Im slightly confused now, could you explain it differently please so I can better assist you? – Greg Feb 02 '13 at 07:39
  • In general imagine I have a small geometry (e.g: star shape), I want to make line between any two arbitrary points with those starts (or any other shape). – Hossein Narimani Rad Feb 02 '13 at 07:56
  • Okay, let me think a little bit. Off the top of my head I think about `Camera Projection` based on your question. Creating a projection plane that will span across your space. Then you simply input `Geometry` of your choice and model it accordingly. I'll try and come up with a solution. – Greg Feb 03 '13 at 17:07
  • A link to my initial thought on your question: http://msdn.microsoft.com/en-us/library/ms747437.aspx#cameras – Greg Feb 03 '13 at 17:09
1

I think this is an interesting problem but I can't fit a satisfying answer in the stackoverflow textbox so I uploaded a proposed solution on github:

https://github.com/mrange/CodeStack/tree/master/q14545675/LineGeometry

I don't claim this is 100% solution to your problem (for one I am not 100% of all your requirements) but if you take a look at it perhaps something can be worked and improved upon.

Unless ofc I am way wrong on what you are looking for.