I have drawing program that the user can draw either an ellipse or a line, which both derive from shape. I am creating one rubber band, and depending on what the user is drawing i say
rubberBand = new Ellipse();
//or
rubberBand = new Line();
but if i set the rubber band to line, I cannot access the x1 x2 etc, it says shape does not contain a definition of X1. I tried creating an Ellipse and the casting it to a line but still same issue. How do I resolve this?
This sounds like a basic polymorphism question to me. Think about what are you actually trying to do- for instance, a line has 2 points (X1/Y1, and X2/Y2). An ellipse (an oblong circle) has no such property- it has a width, maybe, and a height, and possibly an X and Y coordinates (or a position property).
I am guessing that you are attempting to adjust the bounds and/or location of the shape when the user is dragging it with the mouse. In this case, the operations that you need to define for the shape depend on what kind of shape it is. For a line, you need to write a method that adjusts X2 and Y2 (or whatever). For an ellipse, you will probably need another method that adjusts shapes that have width, height, left, and top properties. Then you just need to determine which one to call depending on which kind of shape you are dealing with.
You need to think about Liskov Substitution Principle:
http://www.objectmentor.com/resources/articles/lsp.pdf
http://www.oodesign.com/liskov-s-substitution-principle.html
Related
I need to draw a coordinate system, and inside this coordinate system, I want to draw a polygon based on points from measured data.
When the data changes, the polygon points should change accordingly.
Everything should scale to the size of the "container", although perspective should be kept consistent.
I am new to wpf and binding, so I do not know which part to bind, or to what kind of collection, to be able to reflect changes.
BTW, is there a way to do a general conversion of all coordinate references, so 0,0 will be in the lower left corner, or must everything be calculated seperately?
"is there a way to do a general conversion of all coordinate references, so 0,0 will be in the lower left corner, or must everything be calculated seperately?"
I'm not 100% sure what you mean by this, but because you can bind to the points to a point collection in code behind you can do whatever you like to the points programmatically.
Here is an answer with a good simple example of binding polygon points:
https://stackoverflow.com/a/13571386/1624581
I know X11 has XDrawPoints for drawing multiple points in a batch, but I'd like to have slightly larger points sometimes (useful if you're eg: drawing a line through them), but can't find any way to draw larger points without doing something kludgy like drawing a tiny filled rectangle, or zero-length line with endcaps.
Is there a reasonable way to set the point size that I'm missing?
A point is a single pixel in Xlib. But XFillArcs() would let you create a list of XArc structures that describe small circles. Then you can scale these circles to any radius by setting XArc.width and XArc.height.
I had a generalized question to find out if it was possible or not to do matrix calculations on a rectangle. I have a CvRect that has information stored in it with coordinates and I have a cvMat that has transformational data. What I would like to know is if there was a way to get the Rect to use the matrix data to generate a rotated, skewed, and repositioned rectangle out of it. I've searched online, but I was only able to get information on image transforms.
Thanks in advance for the help.
No, this is not possible. cv::Rect is also not capable of that, as it only describes rectangles in a Manhattan world. There is cv::RotatedRect, but this also does not handle skewing.
You can, however, feed the corner points of your rectangle to cv::transform:
http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html?highlight=transform#cv2.transform
You will then obtain four points that are transformed accordingly. Note that there are also more specialized versions of this function, e.g. warpPerspective() and warpAffine().
For a simplified version of my problem, I would like to calculate the bounding box of a layout-transformed (possibly even render-transformed) shape, so that I can always fit a rectangle perfectly around the shape, no matter what its rotation or scale may be. If you can solve this, I will be happy.
The more complex problem is that of calculating the visual bounding box of any framework element. By 'visual bounding box' I mean the top-most visible pixel within the framework element determines the top-bound, the right-most visible pixel determines the right-bound, etc. If you can solve this, I will be even more happy.
I have tried playing with LayoutInformation.GetLayoutSlot(), but this did not work in the expected manner. The 'layout slot' was actually MUCH larger than the actual bounds. I also tried using VisualTreeHelper.GetDescendantBounds(), but because of the VisualParent of my test shape being protected I could not manage to access this property, and decided to check here before I go any further into it.
I hope that somebody can provide an easy way of getting the true visual bounding box of an element in WPF, that is calculated AFTER all transforms. If I have not made something clear in my question, please let me know.
private Rect GetRectOfObject(FrameworkElement _element)
{
Rect rectangleBounds = _element.RenderTransform.TransformBounds(
new Rect(_element.RenderSize);
return rectangleBounds;
}
Maybe this will help out.
You will get good results with VisualTreeHelper.GetDescendantBounds() and you can use VisualTreeHelper.GetParent() to gain access to the otherwise protected VisualParent property. However what you probably want to do is call GetDescendantBounds on the shape itself, not its parent, because in spite of its name, the method returns the bounds of the parent and all of its decendants.
The problem is not easy, as a control may draw outside its bounds.
But if you assume this doesn't happen you can solve the problem by using parent.TranslatePoint(point_in_child_coord_system, child) to transform (0,0), (child.ActualWidth,0), (child.ActualWidth, child.ActualHeight) and (0,child.ActualHeight) to the parent coord system. Then sort the x and y coordinates of all points and use minimum and maximum values to find the bounding box.
Note: sorting is necessary because of possible child rotation.
I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25).
When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)...
How can I achieve this?
Thanks!
Mark
Just use VisualTreeHelper.HitTest with the callback.
If you have a Viewport3D containing the model, you can just pass in a PointHitTestParameters containing the mouse location.
If you need to operate directly on a Visual3D, pass in a RayHitTestParameters computed from your camera parameters and the mouse location.
In either case your callback will be called with a RayTestHitResult, and if you hit a mesh it will be a RayMeshGeometry3DHitTestResult. This includes a Point3D property telling you the 3D point in space that was hit, and also the mesh and triangle that was hit.
See 3D Hit testing for more details.