I have the following bit of lua code. I am not sure what it is doing
width = aspectRatio > 1.5 and 320 or math.ceil( 480 / aspectRatio )
is it a short circuit?
It is indeed a short circuit. It's equivalence in c would be:
width = aspectRatio > 1.5 ? 320 : math.ceil( 480 / aspectRatio )
Or in english: if the aspect ratio is greater than 1.5, set the width to 320, otherwise set the width to the smallest integral value that is greater than or equal to the division of 480 and the aspect ratio.
Reference
http://www.lua.org/pil/3.3.html
Related
I am new in Corona SDK and I have a problem,
when I place an image at the top of the screen, there is an empty space, how can I make it disappear?
thanks!
It all depends on which device you are running an app. Some of them are taller, some are wider. So there's a value display.screenOriginY which has to be added to a y value if you place an object in a distance from top of screen.
Here's more info:
https://docs.coronalabs.com/api/library/display/screenOriginY.html
In your config.lua file, you can make Corona fill the entire device screen by setting scale = "adaptive". You can read more about adaptive content scaling here
When you use this adaptive scaling, the coordinate (0,0) will be the upper left of the device screen. However, the width and height will vary from device to device and you have to take this into account in your code. You can get these parameters using display.contentWidth and display.contentHeight.
Try (point (0, 0) is located in upper top corner od screen)
--calculate the aspect ratio of the device
local aspectRatio = display.pixelHeight / display.pixelWidth
application = {
content = {
width = aspectRatio >= 1.5 and 800 or math.floor( 1200 / aspectRatio ),
height = aspectRatio <= 1.5 and 1200 or math.floor( 800 * aspectRatio ),
scale = "letterBox",
fps = 30,
imageSuffix = {
["#2x"] = 1.3,
},
},
}
I can't tell if I'm doing something silly or if there is a bug in Corona simulator. When I write the following code:
rect = display.newRect(0, 0, 100, 100)
rect.anchorX = 0
rect.anchorY = 0
rect.x = 0
rect.y = 0
This just sets the anchor point of a 100x100 square to its top left, and sets the position to 0,0. This should make the square snug in the corner, but instead it produces this. It is always a little too far down in the Y axis, but the X axis functions correctly. Does anyone have a fix for this?
With my Corona simulator (build 2016.2992) code works as expected.
main.lua
---------------------------------------------
local rect = display.newRect( 0, 0, 100, 100)
rect.anchorX = 0
rect.anchorY = 0
First of all check your config.lua file. I think it depends on display resolution. For example, in letterbox mode you may have "black bars" on devices with aspect ratios that differ from your content aspect ratio. Read more on Corona documentation.
Below is code from my config.lua file I'm using
config.lua
---------------------------------------------
--calculate the aspect ratio of the device
local aspectRatio = display.pixelHeight / display.pixelWidth
application = {
content = {
width = aspectRatio >= 1.5 and 800 or math.floor( 1200 / aspectRatio ),
height = aspectRatio <= 1.5 and 1200 or math.floor( 800 * aspectRatio ),
scale = "letterBox",
fps = 30,
imageSuffix = {
["#2x"] = 1.3,
},
},
}
I want to understand Aspect Ratio.
Here, I am setting Aspect Ratio of an UIImageView.
These are options when I click this constraint.
How this constraint works and what are "PRESETS", Reverse Multiplier and Convert to Decimal.
Thanks.
Aspect ratio constraint is used to control the width and height of a view as per a aspect ratio that you set here. There are some standard presets such as 1:1 which means width will be equal to height. Similarly other presets calculates the dimensions based on a ratio
Reverse Multiplier is just used to reverse the ratio. E.g. 4:3 will be 3:4
Convert to decimal just represents the ratio as a decimal. E.g. 4:3 will be 1.33
If you want a view to always maintain an aspect ratio then you can use this constraint. In your case if its image view and you know the aspect ratio of the image that will be set then you can set that aspect ratio as the constraint so that the image is always sized according to the image that is set to that image view,
If you select Aspect Ratio for a single item, the width of the item is
used as the numerator for the ratio, and the height is used for the
denominator. If you select Aspect Ratio for multiple items, Auto
Layout chooses the width of one of the items for the numerator and the
height of another item for the denominator. To change the initial
aspect ratio, edit the Multiplier field of the Attributes inspector
for the constraint. To change which item to use for the width or the
height, use the First Item and Second Item pop-up menus in the
Attributes inspector.
Read more here
Constraints are something like equations in maths.
Ex:
let
X- known value (20)
Y- Unknown value (?)
m- multiplier (like 2 or 3 times)
C- constant (+3 or -3)
to find Y value we use this equation.
Y = m * X + C
Y = 2 * 20 + 3
Y = 43
Constraint equation:
First Object = (Multipler * Second Object ) + constant
width = (0.5 * Height) + 20
In Aspect Ratio condition
Note : one value should be fixed ( Height or width )
A) PRESETS
1)Width = 1 * Height
Width/ Height = 1/1 (1:1)
2)Width = 3/4 * height
Width / Height = 3 / 4 (3:4)
B) REVERSE MULTIPLIER
Before Reverse
Width = 1/2 * Height (1:2)
After Reverse
Width = 2/1 * Height (2:1)
C) CONVERT TO DECIMAL
Before conversion
Width = 1/2 * Height
After Conversion
Width = 0.5 * Height (0.5)
I know the distance between the camera and the object
I know the type of camera used
I know the width in pixel on the picture
Can I figure the real life width of the object?
you have to get the angle of camera. For example, iphone 5s is 61.4 in vertical and 48.0 horizontal. call it alpha.
then you calculate the width of object by this way:
viewWidth = distance * tan(alpha / 2) * 2;
objWidth = viewWidth * (imageWidth / screenWidth)
To determine the ratio at which to scale an image, I'm using the following code (borrowed from Trevor Harmon's UIImage+Resize):
CGFloat horizontalRatio = 600 / CGImageGetWidth(imageRef);
CGFloat verticalRatio = 600 / CGImageGetHeight(imageRef);
CGFloat ratio = MAX(horizontalRatio, verticalRatio);
The 600 represents the maximum size I want for the scaled image. CGImageGetWidth and CGImageGetHeight return a size_t which, according to ARC, evaluate to an unsigned long on the iPhone platform (iOS 5).
The problem with the present code is that ratio always evaluates 0.0000.
The width and height of imageRef are actually w=768, h=780, so the ratio should be MAX(0.781, 0.769) = 0.78. How do I this?
P.S. When I used the code above for UIImage's initWithCGImage:scale:orientation: I found that scale works differently than I'd expected: passing in a ratio of 0.78 enlarged the image. Dividing the width or height by the desired size (as in CGImageGetWidth(imageRef) /600, etc.) fixed the problem.
You need one value to be a float to do proper division. Integer division always truncates the floating point numbers. The easiest solution is to turn your numbers into float literals.
CGFloat horizontalRatio = 600.0f / CGImageGetWidth(imageRef);
CGFloat verticalRatio = 600.0f / CGImageGetHeight(imageRef);
CGFloat ratio = MAX(horizontalRatio, verticalRatio);