Rotation problem
Rotation problem
- Subject: Rotation problem
- From: Linea Tessile srl <email@hidden>
- Date: Fri, 10 Nov 2006 18:50:34 +0100
Hi people!
I have a file that contains data as char (-128 to 127) about a design
that I want create in a View with Cocoa.
The file contains X and Y movements as tenths of millimeters (0.1mm).
I used the following code to do it:
#define XFORM_FACTOR 2.58 (pixel dimension of my screen)
#define MM2PX( val ) ( val / XFORM_FACTOR)
float coordX, coordY;
float centerX, centerY;
char buf[10000];
int i=0;
// class to construct the NSbezierpath of type line
LineaC *path;
NSData *data = [NSData dataWithContentsOfFile:pathFile];
[data getBytes:buf];
centerX = 833; //generic point of myView to center the design
centerY = 833;
while (i<10000)
{
coordX = centerX+buf[i];
i++;
coordY = centerY+buf[i];
path = [[ LineaC alloc] initStartPt: NSMakePoint(MM2PX(centerX),
MM2PX(centerY)) toPt: NSMakePoint( MM2PX(coordX), MM2PX(coordY) ) ] ;
[disegno addElement:path];
centerX = coordX;
centerY = coordY;
i++;
}
and it's all ok.
Then I want rotate the design and I use the following code that is ok:
- (void)rotation:(float)grado
{
NSPoint center = NSMakePoint(833,833);
LineaC *elem;
NSPoint st[1];
NSPoint end[1];
int numElem = [disegno count];
NSBezierPath *path;
unsigned int i = 0;
NSAffineTransform *aff, *aff1, *aff2;
// scorro il vettore degli elementi grafici
for ( i = 0 ; i < numElem ; i ++ )
{
aff = [NSAffineTransform transform];
[aff translateXBy:-(center.x) yBy:-(center.y)];
aff1 = [NSAffineTransform transform];
[aff1 rotateByDegrees:-grado];
[aff appendTransform:aff1];
aff2 = [NSAffineTransform transform];
[aff2 translateXBy: center.x yBy: center.y];
[aff appendTransform:aff2];
elem = [ disegno objectAtIndex: i ];
NSBezierPath *path1 = [elem theLine];
path = [aff transformBezierPath:path1];
[path elementAtIndex:0 associatedPoints:st];
[path elementAtIndex:1 associatedPoints:end];
// costruisco un nuovo elemento linea con le nuove coordinate
elem = [[ LineaC alloc] initStartPt: NSMakePoint( st->x,st->y) toPt:
NSMakePoint(end->x, end->y)] ;
[disegno replaceObjectAtIndex:i withObject:elem];
}
}
Well I need to save the design with rotation into a file as byte of
type char:
#define XFORM_FACTOR 2.58
#define PX2MM( val ) ( val * XFORM_FACTOR)
- (void)writeDesign
{
unsigned char *byte;
NSData *myData;
byte = malloc(1);
NSBezierPath *path;
LineaC *elem;
float centerX, centerY;
NSPoint punto[1];
elem = [disegno objectAtIndex: 0 ];
path = [elem theLine];
[path elementAtIndex:0 associatedPoints:punto];
centerX = punto->x;
centerY = punto->y;
int xVal,yVal;
float x,y;
int totale = [disegno count];
for ( i = 0 ; i < totale ; i ++ )
{
elem = [disegno objectAtIndex: i ];
path = [elem theLine];
[path elementAtIndex:1 associatedPoints:punto];
x = PX2MM(((punto->x) - centerX));
y = PX2MM(((punto->y) - centerY));
NSString *xs = [NSString stringWithFormat:@"%.f", x];
NSString *ys = [NSString stringWithFormat:@"%.f", y];
xVal = [xs intValue];
yVal = [ys intValue];
// to have a char (-128 to 127)
if (xVal < 0)
xVal = xVal+256;
if (yVal < 0)
yVal = yVal+256;
*byte = xVal ;
[myData appendBytes:byte length: sizeof(unsigned char)];
*byte = yVal ;
[myData appendBytes:byte length: sizeof(unsigned char)];
centerX = punto->x;
centerY = punto->y;
}
Some movements are wrong as dimension and the effect is a kind of
trasposition (if it is the right name) of all path.
If someone could help me I can then sent an image as example of the
design resulting.
Thanks,
Alessandra
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden