views:

217

answers:

1

hi, I am developing image redraw app. i know the center of one image view which is having scaling property. i.e it can change it frame and center . that to be applied to another view center. when i apply center to newly formed Image view it giving wrong center because of the previous image view is having scaling property. so how can i get exact center to my new image view from previous imageview

A: 

Your view or image is

width*height 

your center view should always be in position

(width/2,height/2) 

whether the image is scaled or not. Just recalculate your center after the scale if you need the "scaled" center or keep in memory the original center position if you don't.

Pseudocode:

getCenter(w,h){
    pos[0]=w/2;
    pos[1]=h/2;
    return pos;
}

calc(image){
    c = getCenter(image.w,image.h);
    scaled = image.scale(80); //That is 80% of original
    d = getCenter(scaled.w,scaled.h);

    if(something) return c;
    else return d;
}

Second explanation after discussion (read comments):

Let's assume you have a 640X480 image and you create a view of 320X240 (a quarter of it) and you move THIS view 100px right and 50 pixels down from position (0,0) which is usually top left corner of your image then:

  • your new center of the VIEW will be as usual in position (160,120) of the VIEW

  • the original center of the ORIGINAL image will remain in its position (320,240) which casually corresponds to the bottom right corner of your VIEW.

  • IF you want to know WHERE the original center of the ORIGINAL image DID end up AFTER movement and "cropping" then you just have to know where did you move the VIEW:

    100px right becomes (original position - relative movement) (320 - 100) = 220 
    50px down becomes (original position - relative movement) (240 - 50) = 190
    

    So your ORIGINAL center will be in position (220,190) of the new VIEW.

Lex
thanks for reply,i am unable to find out center relevant to original after scaling..
maddy
i am able to get center after scaling, but how to know which position in original now
maddy
you just need to do the opposite conversion:if for example you started with a 1280X1024 and got to a scaled 1024X768 image then your scaled center is in position (640,512) your original one can be calculated ONLY IF you know what scaling function was applied.
Lex
yes , i know the scaling function then how can i get original center from scaling center
maddy
for ex:my frame is of first (320*480) its center is (160*240) and after scaling its frame is (1008, 1512), now scaling values are (1.2 ,1.2)then how can i get original (320*480)center
maddy
For example: 1280X1024 to 1024X768 is 1,25:1 for the center, starting with (512,384) is 1:1,25 so: (512,384)->(640,512) A simple multiplication.
Lex
i am unable to get your example. can u explain in details. if possible please post for above my example..
maddy
hi Lex, my question is after scaling my center is moved, for that i need center value in previous
maddy
If your original frame is 320*480 then the new 1008x1512 doesn't have a scaling factor of 1.2,1.2 but of 3.15,3.15 (1008/320).So, old center is (160,240)new center gets so: 160*3.15 = 504240*3.15 = 756that is (504,756) is (1008/2,1512/2) Does this answer your question?
Lex
yes your right its taking scaling factor 3.15,3.15, but my question is after scaling i am moving the image i.e image centre will change, now i want after moving whats the center of original image
maddy
oh, then you have to sum up for the moving:example: original center (160,240) scaled AND moved (1008,1512) moved for 63 px to the right (so center gets +100px on width)You have first to correct movement then rescale: origCenter = ((w-63)/scale)/2 : ((1008-63)/3.15)/2 = 150 So, your actual original center is in position 150 as for X, Y is identical
Lex
thanks for info. its not working is there any alternative
maddy
i know the present center i.e after scaling center from this how i can get center of previous..
maddy
HI lex . is there any another way..?
maddy
I don't know if there's another way, the only one I can think of is just go back in scaling but I think that's not what you are trying to achieve: in fact if you got a scaled image of 1008X1512 from an ORIGINAL one of 320X480 then you already have your OLD center (ie:160,240) the NEW one being (504,756), if you want to go back from this just divide it by the scale 504/3.15 = 160. But I think this is not what you want.
Lex
thanks for your valuable information.
maddy
i think its my fault , here i am not getting center exactly half of the view, is there any another way without center to know how much moved in original after scaling View
maddy
I'm editing my answer to add more explanation
Lex