Does a Silverlight <Line> need Width and Height defined? - silverlight-3.0

It seems that a won't be visible unless a height and width is specified. Is this true?
This seems rather tedious. Couldn't the bounds of the line imply the correct width and height?
<Canvas Height="200" Width="200" HorizontalAlignment="Left" Background="Beige">
<!-- This is visible -->
<Line X1="20" Y1="40" X2="70" Y2="90"
Stroke="Black" StrokeThickness="4" Height="100" Width="100" />
<!-- This is not visible -->
<Line X1="10" Y1="10" X2="50" Y2="50"
Stroke="Black" StrokeThickness="4"/>
</Canvas>
(I'm using Silverlight 3.)

In SL 4, MS shows examples of mixing X1, X2, Y1, Y2 with Canvas.Left
In my own experience, with Visual Studio 2010, in order to see the line in the designer view, I had to set non zero width and height, while also using X,Y to set the line within that space.

No, you can use one of this templates to define coordinates:
X1, X2, Y1, Y2
Canvas.Left, Canvas.Top, Width, Height.
If you use them together, CLR may sometimes show your controls wrong.

It seems that the line is drawn at a point (X1, Y1) relative to the Canvas.Left, Canvas.Top of the Line element. So not specifying the Canvas.Left, Canvas.Top means that the line is actually drawn at the point (X1, Y1).
It's like the control dimensions (or the drawing surface of the line) is determined by the Canvas.Left, Canvas.Top, Width, Height and the actual line is render by (X1, Y1) and (X2, Y2) relative to Canvas.Left, Canvas.Top.
The RotateTransform uses the Canvas.Left, Canvas.Top values (unless another Center is specified) and not the X1 etc values. So any transforms on the line may not work as expected if those values differ.

I have silverlight 4, and I resolved the problem specifing the canvas width, canvas height, x1, x2, y1, y2.

Related

Positioning and Resizing images in different aspect ratios keeping the same position relative to the screen

the problem I'm facing is more a matter of logic and algorithm than a specific language functionalities, i'm coding it in lua, but i believe it could be replicated in other languages with no major problems.
First of all, I'm going to show you some properties and default settings that i'm having to use to come up with a solution.
1. I have a general function that displays an image on the screen, given the X, Y position and W, H dimension, to facilitate understanding, this function is drawImage(x, y, w, h)
2. All values ​​and calculation will be based on a default resolution and aspect ratio, which in this case is the developer's. These variables will be these: DEV_SCREEN_W = 1366, DEV_SCREE_H = 768, (aspect ratio is 16:9)
3. So far, we have a function that displays an image on the screen, and default screen values ​​to which the X, Y position and W, H dimensions of a given image will be set.
4. Now, we have the CLIENT, which can be anyone, with any resolution and aspect ratio, this client will run the code on his computer.
5. Knowing this, we need to make an algorithm, so that the positions and dimensions of the image stay relatively the same regardless of the screen being used to show it.
Knowing these properties and definitions we can proceed with the problem. Let's assume that me as a developer, having a screen whose values are DEV_SCREEN_W = 1366, DEV_SCREE_H = 768 i want to set an image at position X = 352, Y = 243 with W = 900, H = 300. So At the developer screen, i'll have this:
Okay, now let's add one more image, with position and dimension X = 352, Y = 458, W = 193, H = 69
Okay, now we need to write an algorithm that keeps the same dimension and position on the screen regardless of size, as W and H are different for each resolution, we can't use pixel points to define, my solution was to define the position between 0 and 1, so the position would represent a certain percentage of the screen, the same for the W and H.
Let's suppose i get the screen information from the client and I get CLIENT_SCREEN_W = 1280, CLIENT_SCREEN_H = 720.
Since it's the same aspect ratio, I could apply this concept to both position and dimension as it would remain perfectly proportional to the screen, so i would have:
Getting the percentage based on the DEV screen for BOTH images would be like:
X = 352/DEV_SCREEN_W * CLIENT_SCREEN_W,
Y = 243/DEV_SCREEN_H * CLIENT_SCREEN_H,
W = 900/DEV_SCREEN_W * CLIENT_SCREEN_W,
H = 300/DEV_SCREEN_H * CLIENT_SCREEN_H,
Basically, for those who didn't understand what is happening, i get the data of how many % position in pixels represents from the developer's screen (X = 352/DEV_SCREEN_W) that is (X = 352/1366 = 0.2576) and multiply this result by the W of the client screen: 0.2576 * CLIENT_SCREEN_W, that is 0.2576 * 1280 = 329. Thus, we concluded that 329 and 352 are relatively the same position in different resolutions.
Following this concept, no matter what resolution the client uses, the images are always in the same proportion, both in position and in dimension, ONLY IF IT IS IN RATIO 16:9 (the same as the developer)
And this is where the problem arises, applying this same concept to any ratio, on a 4:3 screen the both image would be stretched:
despite keeping the same X and Y relative to the screen, the W and H had to be altered out of proportion to fit the screen, obtaining the result seen above, which cannot happen.
To avoid this, i set a proportion rate, which i get by dividing the client's screen by the dev's, thus getting how much of one represents the other, and i multiply that by W and H of both images so that both are proportionately resized to their original dimension, instead of multiplying by a relative value between 0 - 1 arbitrarily.
Getting the proportion would be like:
PROPORTION_RATE = CLIENT_SCREEN_W/DEV_SCREEN_W
Applying it:
W = 900*PROPORTION_RATE, H = 300*PROPORTION_RATE
Basically, this multiplication for aspect ratio, makes the image stay in the exact proportion of the screen resizing it, however, applying this, the images lose their relative position, as seen in the image below:
As you can see, despite keeping the same proportion in W and H, the image lost its structural organization in relation to the original position defined on the developer's screen.
I've been in this problem for a while
The closest I got was to add on the Y and X axis how much a certain image has decreased, however, if i do that both images will be corrected, but they would still be out of relative position between them, as shown in the image below:
[]
This problem of logic and algorithm is a little beyond my applicable knowledge, alone I can't find a solution, so I sincerely ask for help, or direction to the way where I can solve it.
Based on your comments, you want to scale both axes by the same amount, and you want to handle ratio mismatch by adding empty stripes at window edges as needed.
First you compute the scale: scale = min(w2/w1, h2/h1), where w1,h1 is the source size, and w2,h2 is the target size.
Next, assuming 0,0 is in the center of the screen, you can just do x2 = x1*scale, y2 = y1*scale, where x1,y1 are source coordinates and x2,y2 are converted coordinates.
But if 0,0 is in a corner of the screen for you (which is more probable), you have to do something like:
offset_x = (w2 - w1 * scale) / 2
offset_y = (h2 - h1 * scale) / 2
Then:
x2 = x1 * scale + offset_x
y2 = y1 * scale + offset_y

How to reset a Rectangle's x,y,width,height, after transform

I have an app that uses konvajs, where I set rectangles to be resizable. I have it set such that after I transform the rectangle I set the scaleX and scaleY to 1 so I can just use x, y, width, and height. I do this with the following code:
myRectangle.on('transformend', function() {
myRectangle.width(Math.round(myRectangle.width() * myRectangle.scaleX()));
myRectangle.height(Math.round(myRectangle.height() * myRectangle.scaleY()));
myRectangle.scaleX(1);
myRectangle.scaleY(1);
});
However, sometimes after I resize (usually if I "flip" the rectangle by dragging up or to the left), the x, y, width and height are strange values. Sometimes the width or height is negative, sometimes it seems like the x and y positions do not refer to the top left of the rectangle. I want to be able to extract information about the rectangle, so I would like position to be top left of the rectangle with positive width and height values. I don't mind resetting these values after the rectangle is tranformed, but I am not quite sure how konvajs is calculating the x,y,width, and height so I can't properly reset them. Is there some metric indicating when a tranform "flips" a rectangle? Or some other way to reset it?
It seems that setting flipEnabled and rotationEnabled to false on the transformer prevents rotations from happening.
To get a visual sense of what is happening to the attrs during the transform, take a look at the demo in the official docs here and pay special attention to width/height, rotation and scale as you resize by dragging the right edge first, then repeat with the bottom edge.
It will help to understand that dragging a Transformer handle changes the scale of the rectangle - not the width or height. However this is not the end of the story - if you 'flip' the shape in the horizontal axis then you will see that the rotation is changed from zero to 180 degrees and the scaleX remains positive. But if you drag and flip the shape in the vertical axis then there is no rotation effect and the scaleY switches to negative.
Long story short - at the moment I can't think of a useful use-case that requires trying to redraw the rectangle without scale or rotation affects, which I will refer to as the 'plain' rect versus the 'exotic' rect you get after using the Transformer.
If the use-case is hit detection via your own math then you have everything you need to know in the rects x & y, width & height, rotation and scaleX & scaleY. Even if you could get the attrs for a plain rect you would still have the same params to plug into your math, so recomputing the plain rect is wasted effort.
If the use-case is storage (serialization) of the rect's attrs then again the same point as above - you need to store the position, rotation, size, and scale so as to be able to redraw it later.
A legitimate use-case for resetting scale to 1 would be if your app's business case requires it. But this only covers resetting:
rect.seAttrs({
width: rect.width() * scaleX,
height: rect.height() * scaleY,
scaleX: 1,
scaleY: 1
}
and leaves the rect at the same position and rotation.
Conclusion: attempting to recompute a plain rect from an exotic rect may not be worth the effort in some cases.

How to draw a horizontal line using prawn/rails on X axis at a specific point

A user can specify a margin-bottom for a page and I want to be able to draw a horizontal line showing where they have placed it. I have defined margin_bottom in my PDF document (A4 size).
All I need to do is add the specified margin to the X axis and draw a line. However, how can I draw this line?
From my knowledge, I could call something like this
stroke_horizontal_line(0, 0 + margin_bottom)
However, this isn't working and I need it to be the full width of the page.
First of all, I think stroke_horizontal_line works a bit different than you think (if I'm understanding what you are trying to do correctly)
stroke_horizontal_line(x1, x2, at: y) Draws a horizontal line from x1 to x2 at the vertical height of y, where y starts at 0 at the bottom of your document (after bottom margin, not at the bottom of the page).
So, I think you are trying to draw a horizontal line across the whole page, at a specific height which is defined by some variable margin_bottom, to accomplish that with stroke_horizontal_line you can do this:
# You can set a stroke color, if you set it to white it is possible that you just
# forgot to set it back to black.
stroke_color 0, 0, 0, 100
# You can also set the line width
line_width 2
# This would draw a horizontal line across the whole page (not over your side margins)
# at the height of margin_bottom + your document bottom margin (measured from the bottom up)
stroke_horizontal_line(0, bounds.width, at: margin_bottom)
You can also use stroke_horizontal_rule, which just draws a horizontal line at you current cursor position, so you would have to place the cursor on the right spot before using that method.
Easy.
stroke_horizontal_line (y), (y), at: (x)
stroke_horizontal_rule
Should do it.

Svg simple transformations

I have a simple svg with 3 paths: red, blue and yellow.
I want to: scale all shapes (eg. 0.3) and rotate only the red + blue(eg.90 deg).
The rotation point should be the middle of red path.
After these operations I want the yellow shape to have the distance to the red path the original distance scaled by 0.3.
My attempt was:
compute the middle of the red path;
translate in the origin (0,0),by translated with (-redCenterPoint.x, - redCenterPoint.y)
Scale red path by 0.3
move back red path by translate(redCenterPoint.x, redCenterPoint.y)
repeat the same for blue and yellow by computing blueCenter, yellowCenter
My question is: How can I keep the original image structure but scaled by 0.3 and rotate by 90? - blue path to be in touch with red path and yellow to have original distance scaled by 0.3.
I saw that if I consider the redCenterPoint for all 3 shapes then the group looks the same as original but scaled, looks correct.
I want to know to do the same but with the first method.
svg file:
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<path
style="opacity:1;fill:#ff0000"
d="m 146.98669,417.50473 561.36408,0 0,206.40686 -561.36408,0 z"
id="red"
inkscape:label="#table" />
<path
style="opacity:1;fill:#0000ff"
d="m 641.11218,339.32031 65.67491,0 0,82.87548 -65.67491,0 z"
id="blue" />
<path
style="opacity:1;fill:#ffff00"
d="m 764.69525,515.63883 55.28473,-55.28473 46.43917,46.43918 -55.28473,55.28472 z"
id="yellow"
inkscape:connector-curvature="0"
inkscape:label="#yellow" />
code in delphi using Riversoft component for renderin SVG:
redBounds: TSVGRect;
redCenterPoint: TPointF;
redMatrix: TSVGMatrix
redBounds := (svgDoc.SVG.AllItems['red'] as TSVGGraphicElement).BoundsRect;
redCenterPoint.x := bDiamond.Left + (bDiamond.Width) / 2;
redCenterPoint.y := bDiamond.Top + (bDiamond.Height) / 2;
redMatrix := CreateTranslateRSMatrix(-redCenterPoint.x, -redCenterPoint.y);
redMatrix := RSMatrixMultiply(redMatrix,
CreateRotationRSMatrix(TPoint(0,0), DegToRad(90)));
redMatrix := RSMatrixMultiply(redMatrix,
CreateScaleRSMatrix(0.3, 0.3));
redMatrix := RSMatrixMultiply(redMatrix,
CreateTranslateRSMatrix(redCenterPoint.x, redCenterPoint.y));
(svgDoc.SVG.AllItems['red'] as TSVGGraphicElement)
.Matrix := mainMatrix;
What you are doing is keeping the centre constant for each individual shape. That is not what you want to do. You want to keep the centre of the whole shape constant. That is what you are doing in your second attempt where you apply the red centre to all 3 shapes, which is why it looks better. You might want to choose a different centre, and there are various algorithms for doing that, or even leave the origin where it is (not bothering to move the centre at all). But you must apply the same centre, whatever you choose, to all shapes to achieve what you want, and that applies to rotation too (although in your case you only apply rotation to two of the shapes). The centre for rotation, though, does not have to be the same as the centre for scaling.
For the most effective operation for calculating your new centre in each case I thing I would use centre of gravity calculations, but just simple average of centres would work well too.

Correct icon sizes for launch screen

Following from njuri's response below, I have added a new image asset and placed within it three images: 200 x1, 400 x2, and 600 x3 (please see image below):
Launch Screen in Xcode for Swift
Is this correct? I wasn't sure if you meant 200 x1, 400 x2 and 600 x3 or 200 x1, x2, x3; 400 x1, x2, x3; 600 x1, x2, x3 (in other words 3 images or 9 images).
Also, I have the 'Universal' box checked. Is that correct?
Thanks.
When working with asset catalogs select the placeholder for the asset you will be using and the inspector on the far right bottom will tell you what the expected size. Repeat this for every image you intend to use to find out the size you need.

Resources