Thanks Alain,
Its not quite there but its close.
I've been getting my butt kicked trying to understand why scaling
affects drawing the way it does. I have to compensate for scaling.
Last week I had some elaborate method for doing what I did and just
as I thought it was going to work, poof. It didn't work.
Thank you much for you effort. I hope to get it to work soon.
W.
-----Original Message-----
From: Alain Pastor [mailto:apastor@...]
Sent: Tuesday, March 01, 2005 5:53 AM
To: futurebasic@...
Subject: Re: [FB] scaling, centering and rotating
Edwards, Waverly a écrit :
> I'm probably missing the obvious but I'm having a hard time centering
> a rect within a rect. The coordinate system I'm using is the same as
> CG not QD where the Y axis is upside down. When I scale the rect it
> becomes more and more off center. I also planned on rotating the
> object from its center but I haven't gotten past getting the object
> scaled and centered.
>
> Any help would be much appreciated.
>
Please, don't even try to ask me why, but your example *seems* to work
doing the following (I don't think it is correct, though):
include "Tlbx CoreGraphics.incl"
local fn rotateOnCenter( ctx as CGContextRef , sRect as ^CGRect, dRect
as ^CGRect, angle as single )
dim as single left,top,right,bottom
dim as CGPoint p
left = sRect.origin.x
top = sRect.origin.y
right = left + sRect.size.width
bottom = top + sRect.size.height
// draw our RECT so that top of RECT is at the port origin. To do this
// we offset our rect so that the Y value is at (portHeight -
drawRectHeight)
fn CGRectOffset(sRect, sRect, 0, (bottom - top) - sRect.size.height)
// in case we want to rotate about the center of the RECT
p.x = sRect.origin.x + sRect.size.width\2
p.y = sRect.origin.y + sRect.size.height\2
fn CGContextTranslateCTM(ctx, p.x, p.y)
fn CGContextRotateCTM(ctx, -angle)
fn CGContextTranslateCTM(ctx, -p.x, -p.y)
end fn
LOCAL
dim tmpR as CGRect
dim as single t,l,b,r
dim as single tt,ll,bb,rr
DIM as single cy,cx
LOCAL FN centerCGRect( ctx as CGContextRef, sRect as ^CGRect ,dRect as
^CGRect, scale as single )
tmpR = sRect
l = sRect.origin.x
t = sRect.origin.y
r = l + sRect.size.width
b = t + sRect.size.height
ll = dRect.origin.x
tt = dRect.origin.y
rr = ll + dRect.size.width
bb = tt + dRect.size.height
FN CGContextSetRGBStrokeColor( ctx, 1, 0, 0, 1 )// show centered rect in red
fn CGRectOffset( sRect, sRect, -l, -t )// offset to zero
//cx = (rr + ll)\2// horz center
//cy = (bb + tt)\2// Vert center
//fn CGRectOffset( sRect, sRect, cx-r\2, cy-b\2 )//Center in Rect
cx = (dRect.origin.x + dRect.size.width\2)\scale - sRect.size.width +
dRect.origin.x\scale
cy = (dRect.origin.y + dRect.size.height\2)\scale - sRect.size.height +
dRect.origin.y\scale
fn CGRectOffset( sRect, sRect, cx, cy )//Center in Rect
FN CGContextTranslateCTM( ctx, sRect.origin.x , sRect.origin.y )
FN CGContextScaleCTM( ctx, scale, scale )// use natural coordinate
system, not flipped as in QD
fn CGContextSetLineWidth(ctx, 1\scale)
//FN CGContextStrokeRect( ctx, tmpR )
FN CGContextStrokeRect( ctx, sRect )
END FN
local fn drawCenteredRectsBetter( ctx as CGContextRef , arbitrarySrc as
long )
'~'1
dim as CGRect cgR,cgRR
dim as single left,top,right,bottom
dim as single hh,wd,scale
fn CGContextSaveGState( ctx )
long if ( arbitrarySrc )
// arbitrary source in arbitrary destination
left = -89.57098
top = 36.49728
right = -81.96504
bottom = 39.14807
fn CGRectMake( cgRR, 100, 100, 400 , 300 )
xelse
left = 10
top = 10
right = 20
bottom = 20
fn CGRectMake( cgRR, 0, 0, 800 , 600 )
end if
wd = right - left
hh = bottom - top
scale = 10// how much do we want to scale the object?
fn CGRectMake( cgR, left, top, wd , hh )
FN CGContextSetRGBStrokeColor( ctx, 0, 0, 1, 1 )
fn CGRectInset( cgRR, cgRR, 20, 20 )// shrink destination rect
FN CGContextStrokeRect( ctx, cgRR )// display destination rect
FN centerCGRect( ctx, cgR ,cgRR, scale )
fn CGContextRestoreGState( ctx )
fn CGContextFlush( ctx )
end fn
local fn createWindowAndContext( windNum as long, name as str255 )
'~'1
dim as CGContextRef @ ctx
dim as OSStatus err
dim as CGRect cgR
appearance window windNum, name , (0,0) - (800,600),
_kDocumentWindowClass, 0
err = fn QDBeginCGContext( window( _wndPort ), @ctx )
fn CGRectMake( cgR, 0, 0, window( _width ), window( _height) )
// draw a light gray background
fn CGContextSetRGBFillColor( ctx, 0.85, 0.85, 0.85, 1.0 )
fn CGContextFillRect( ctx, cgR )
fn drawCenteredRectsBetter( ctx, _false )
//fn drawCenteredRectsBetter( ctx, _true )
fn CGContextFlush( ctx )
err = fn QDEndCGContext( window( _wndPort ), @ctx )
end fn
fn createWindowAndContext( 33 , "test" )
do
handleevents
until 0
--