Declare to CoreGraphics CGAffineTransform

Hello, I’m trying to implement The CoreGraphics CGAffineTransform functions via declares and am having some trouble (I’m new to declares.)

Reference for CGAffineTransform: Apple Developer Documentation

I have a class with a structure that maps to CGAffineTransform:

This should be correct: 48 bytes (6 doubles), column-major format. I’m only running 64-bit code.

My class has a property, “Matrix as CGAffineTransform”, and initializes it to the identity transform:

Me.Matrix.a = 1.0
Me.Matrix.d = 1.0

I’ve also set up a log method:

Dim s As String = EndOfLine

s = s + Me.Matrix.a.ToString("0.0") + "   " + Me.Matrix.b.ToString("0.0") + EndOfLine
s = s + Me.Matrix.c.ToString("0.0") + "   " + Me.Matrix.d.ToString("0.0") + EndOfLine
s = s + Me.Matrix.tx.ToString("0.0") + "   " + Me.Matrix.ty.ToString("0.0") + EndOfLine


If I log the output of the identity matrix I get the expected result:

1.0   0.0
0.0   1.0
0.0   0.0

I’m writing and calling my declare like this:

Declare Function CGAffineTransformTranslate Lib CoreGraphics (t As CGAffineTransform, tx As CGFloat, ty As CGFloat) As CGAffineTransform

Me.Matrix = CGAffineTransformTranslate(Me.Matrix, tx, ty)

Reference for the CoreGraphics function: Apple Developer Documentation

But the result not correct:

1.0   0.0
0.0   4.0
0.0   0.0

To verify, this is an Objective-C version:

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>

int main(int argc, char *argv[]) {
	@autoreleasepool {
		CGAffineTransform t = CGAffineTransformIdentity;

		int s = sizeof(CGAffineTransform);
		NSLog(@"sizeof CGAffineTransform: %i\n\n", s);

		NSLog(@"%f   %f", t.a, t.b);
		NSLog(@"%f   %f", t.c, t.d);
		NSLog(@"%f   %f\n\n", t.tx, t.ty);

		t = CGAffineTransformTranslate(t, 3, 6);
		NSLog(@"%f   %f", t.a, t.b);
		NSLog(@"%f   %f", t.c, t.d);
		NSLog(@"%f   %f\n\n", t.tx, t.ty);

…which produces the output:

sizeof CGAffineTransform: 48

1.000000   0.000000
0.000000   1.000000
0.000000   0.000000

1.000000   0.000000
0.000000   1.000000
3.000000   6.000000

Any tips would be appreciated!


I can’t help you with the declares, but I can tell you that the math behind affine transformations is actually relatively simple despite the apparent complexity. If you are only using the values within Xojo, it might be easier (and more cross-platform) to write your own function.

Posting a link to the project:

That’s true, Eric, but the CG transform functions offer a lot of well-tested, bug-free functionality from the start: matrix concatenation, multiplication, etc. — if I can get it working. :thinking:

And… fixed. My matrix structure was transposed.

Apple’s picture of the matrix was the transposition of the text description of the matrix.

1 Like

Awesome! Congrats.

1 Like