LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2017, 02:01 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 08-29-2017 at 02:02 PM.
 
Old 08-29-2017, 02:20 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Since w * aspect ==> 1600 your aspect is 1.666. Printing as an int this is 1. Looks right to me.
 
Old 08-29-2017, 02:21 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
This is what I have now:
Code:
    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);

Last edited by BW-userx; 08-29-2017 at 02:30 PM.
 
Old 08-29-2017, 02:40 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I hate math:

Code:
   imlib_blend_image_onto_image (img.image, 0, 0, 0, h,w, (img.width - hh) / 2, (img.height - ww) / 2, (int) (h * aspect) / 2, (int) (w * aspect) / 2);
the heck with all of that middle stuff. I just shoved it in the function call.

Last edited by BW-userx; 08-29-2017 at 03:59 PM.
 
Old 08-29-2017, 06:32 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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.
 
Old 08-30-2017, 07:35 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by astrogeek View Post
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.

Last edited by BW-userx; 08-30-2017 at 08:07 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: PayPal Payment Method Is Coming To Ubuntu Software Center LXer Syndicated Linux News 0 12-09-2011 03:00 AM
LXer: Addictive Game 'Family Farm' for Linux Available Now, Coming Soon to Ubuntu Software Center LXer Syndicated Linux News 0 04-23-2011 12:40 PM
Black screen (power saving) is not coming up on login screen Metaleks Red Hat 1 11-24-2008 04:05 PM
How Do I Center Windows On Screen? taurusx5 Linux - Software 4 09-23-2008 04:12 PM
Help..Suse 9.3 kde control center not coming up x25 Linux - Software 1 09-22-2005 01:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration