[SOLVED] center screen algorithms coming up with zero's whe it shouldn't
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
center screen algorithms coming up with zero's whe it shouldn't
I got this image it gets flipped 180, then this Now scrambled algorithm becuse it was not centering the image nor using the complete image, it was chopping off the length on flip, I got that chopping it off fixed. But I cannot get past this math algorithm that should not be giving me zeros for x and y.
at least it doesn't on my calculator or in my head.
I've been just working on x for now, but y too is screwed up.
Code:
// img.width = screen width
// img.height = screen height
double aspect;
aspect = ((double) img.width) / w;
if((int)(h * aspect) > img.height)
{
aspect = (double) img.height / (double) h;
}
x = (img.width / 2) - ((int)( w * aspect) / 2); // x left
// should be this but it is not
// x = (1600 / 2) - (( 960 * 1 ) / 2);
// WHICH IS
// 320 = 800 - 480
y = (img.height - (int) (h * aspect)) / 2; // y top
I get zero.
printf so you know how I got it to get the values.
Code:
printf("screen width /2 %d\n", (img.width / 2));
printf("img width %d\n",w);
printf("aspect %d\n", (int)aspect);
printf("(int)( w * aspect) %d\n", (int)( w * aspect));
printf("img width ((w * aspect) / 2) %d\n", ((int)( w * aspect) / 2));
printf("end result %d\n", (img.width / 2) - ((int)( w * aspect) / 2));
// results
screen width /2 800
img width 960
aspect 1 <---
(int)( w * aspect) 1600
image width ((w * aspect) / 2) 800 <---- 960 * 1 / 2 is not 800
end result 0
some how its getting bad math.
Code:
960 * 1 = 960
960 / 2 = 480
not 800
800 - 480 = 320
// that is what I should be getting with this image anyways.
if((int)(h * aspect) > img.height)
{
aspect = (double) img.height / (double) h;
}
printf(" apecst %d\n", (int)aspect);
// changed to ww and hh so the actual image w h does not screew up the image when setting to screen.
/ but this divided by 2 and subtract is still not setting it center screen.
ww = (int) (w * aspect) / 2;
hh = (int) (h * aspect) / 2;
// hh ww gives me the new image size that it needs to work with to get center x y
printf("w %d h %d\n", w,h);
x = (img.width / 2) - ((int)( ww * aspect) / 2);
y = (img.height / 2) - ((int) (hh * aspect) / 2); // y
imlib_blend_image_onto_image (img.image, 0, 0, 0, h,w, x,y, (int) (h * aspect) / 2, (int) (w * aspect) / 2);
Please do not change the conditions, and hence the question, unless it provides an obvious step towards a solution. It leaves others chasing a moving target.
From your original question which is all I have had time to digest, the math is returning the correct answer, zero.
The apparent essential facts extracted from the original post are:
Code:
img.width = implied value is 1600
img.height = ?
w = implied value is 960
h = ?
double aspect = img.width / w = implied value is 1.666..
We cannot tell how the if(..) condition is evaluated by data given - we have no value given or implied for h or img.height.
Code:
if(h*aspect)>img.height
aspect = (double) img.height / (double) h;
x = (img.width / 2) - ((int)( w * aspect) / 2)
But solving for x with the implied values:
Code:
x = (img.width / 2) - ((int)( w * aspect) / 2)
OR
x = (1600 / 2) - ((int)( 960 * 1.666)/2) = 800 - 800 = 0
The math is correct as given, so your algorithm must be incorrect if that is not the answer you were expecting.
Please do not change the conditions, and hence the question, unless it provides an obvious step towards a solution. It leaves others chasing a moving target.
From your original question which is all I have had time to digest, the math is returning the correct answer, zero.
The apparent essential facts extracted from the original post are:
Code:
img.width = implied value is 1600
img.height = ?
w = implied value is 960
h = ?
double aspect = img.width / w = implied value is 1.666..
We cannot tell how the if(..) condition is evaluated by data given - we have no value given or implied for h or img.height.
Code:
if(h*aspect)>img.height
aspect = (double) img.height / (double) h;
x = (img.width / 2) - ((int)( w * aspect) / 2)
But solving for x with the implied values:
Code:
x = (img.width / 2) - ((int)( w * aspect) / 2)
OR
x = (1600 / 2) - ((int)( 960 * 1.666)/2) = 800 - 800 = 0
The math is correct as given, so your algorithm must be incorrect if that is not the answer you were expecting.
I noticed the bad algorithm after I posted having gotten a different look at it, which made me feel like a bad person for being human. But I waited 5 to 6 hours having worked on this. Then as time does it went by and next thing I knew 5 or 6 hours went by, I got tired of it and had seriously talk myself into posting for a different pair of eyes to look at it, which took another hour to do that.
went back to working on it, added ww hh to get the values separated because w and h had to stay the same with the other math and using it for x and y like that screwed up the image size so I separated it ww hh
then looking at it again in the post getting a different perspective on it, I just happened to noticed what I was doing wrong,
when I noticed my mistake after changing it to ww hh by then brained drained I just shoved what was needed into the function call. Eliminating all of that middle stuff that was for whatever reason kept screwing me up then it just worked, so I sat and said to myself. Self, dang, I should have done that 7 hours ago. What a waist of a day.
Then I posted the SOLUTION so someone might see it and learn from my Mistakes,
marked it solved. Called it a day then went back to something else that needed to be done on this.
I was only working on x because y was working, I only needed to fix x
the ww and hh were added to get the values separated because it was screwing up the image size values needed when setting the image.
then today I see your post, after I marked it solved.
day late and a dollar short.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.