How to convert integer to float value

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

How to convert integer to float value

#1 Post by Imperf3kt »

I'll start by saying I have no idea where I should post this. Its probably best suited to the Ren'Py cookbook, but its not really Ren'Py related, its just basic math.

Anyway, I recently put together (still putting together) a GUI in which I was asked to change the resolution from 1920x1080 to 1280x720.

Now, in some screens I had done a lot of pixel precise work and used integers, which obviously had to be changed for a different resolution.
So, thinking about what to do, I thought I'd just use float values instead.

To simplify things, say I had an image positioned using integers. For example at 100 pixels on the x axis, and 900 pixels on the y axis.

Code: Select all

    xpos 100
    ypos 900
    
On a 1920x1080 screen, this is fine, but when we change that to 1280x720, suddenly the image is off the screen!
A simple solution is to use a float value instead, which will be a percentage of the resolution, instead of a fixed number. So, 0.5 for example, is half the resolution. For 1920x1080, 0.5 would be 960 on the x axis, or 540 on the y axis, and yet for 1280x720 its different, being xpos 640, ypos 360.

So to salvage all your work, and convert an integer to float, all you need to do is take your integer and divide it by your resolution.

For example, in the previous example, an image at 100, 900 would become:
xpos= 100/1920
ypos = 900/1080

Code: Select all

    xpos 0.0520833333333333
    ypos 0.8333333333333333
    
You should probably round this off to three numbers using the Swedish rounding system.

Code: Select all

    xpos 0.052
    ypos 0.834
    
Using this simple trick, can save you a lot of work repositioning everything if you ever find yourself needing to adjust the resolution. It works in reverse too, if you decide to increase your resolution and future-proofs your work.
Or you can use it to find pixel precise positioning in an image editor, and input the value as a float.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
磯七ラスミ
Regular
Posts: 65
Joined: Tue Nov 29, 2016 4:31 pm
Projects: If Facts Aren't Alibi | 盲点に日光 (Mōten ni Nikkō) | Nest, Treetop | Untail Wire Stretch
Contact:

Re: How to convert integer to float value

#2 Post by 磯七ラスミ »

How about using xalign and yalign?
I'm not sure when you may use one number notation or the other.
Another idea I have is looking for the variable that stores the size of screen. Maybe config.screen_width and config.screen_height?

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to convert integer to float value

#3 Post by Imperf3kt »

xalign and yalign are neither integers, nor floats. They are positional arguments that take either an integer or a float value, and thus won't help at all (if you used an integer, it will still require changing for the different resolution, or you could convert it into a float, exactly what this thread is designed to help people do.)

This is not a question, but rather a 'how to' for others, but thanks for the thoughts.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Post Reply

Who is online

Users browsing this forum: No registered users