my apologies for posting an Objective-C question to the C section... I thought maybe some of you would also be versed in the ways of Objective-C... please, you don't even know what a dearth of help for Objective-C students exists out there... have pity.
I am trying to call a method of one class from inside another classes' method. Specifically, I have a method that can set the x and y points inside a class called XYPoint. This defines the "origin" of a shape, like a rectangle. I have another Rectangle class that uses the XYPoint class.
When I call the XYPoint set method... it works fine.
[point1 setX: 50 setY: 50];
but inside the Rectangle class...
[ptB setX: self.origin.xPoint setY: (self.origin.yPoint + self.height)];
it doesn't work. I have included the truncated program below... I also tried allocating and initializing my "ptB" variable... didn't help. I'm lost...
Code:
#import <Foundation/Foundation.h>
//XY POINT
@interface XYPoint: NSObject
-(double) xPoint;
-(double) yPoint;
-(void) setX: (double) x;
-(void) setY: (double) y;
-(void) setX: (double) x setY: (double) y;
@end
@implementation XYPoint
{
double xPoint;
double yPoint;
}
-(void) setX: (double) x
{
xPoint = x;
}
-(void) setY: (double) y
{
yPoint = y;
}
-(double) xPoint
{
return xPoint;
}
-(double) yPoint
{
return yPoint;
}
-(void) setX: (double) x setY: (double) y;
{
xPoint = x;
yPoint = y;
}
@end
// RECTANGLE
@interface Rectangle: NSObject
-(double) width;
-(double) height;
-(void) setWidth: (double) w;
-(void) setHeight: (double) h;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) setWidth: (double) w setHeight: (double) h;
-(double) area;
-(double) perimeter;
-(void) translate: (XYPoint *) pt;
-(BOOL) containsPoint: (XYPoint *) aPoint;
-(Rectangle *) intersect: (Rectangle *) rect;
-(XYPoint *) origin
{
return origin;
}
-(void) setOrigin: (XYPoint *) pt
{
origin = pt;
}
-(BOOL) containsPoint: (XYPoint *) aPoint
{
BOOL inX, inY;
if (self.origin.xPoint < aPoint.xPoint && aPoint.xPoint < self.width)
{
inX = TRUE;
}
else
inX = FALSE;
if (self.origin.yPoint < aPoint.yPoint && aPoint.yPoint < self.height)
{
inY = TRUE;
}
else
inY = FALSE;
if (inX && inY)
{
contains = TRUE;
}
else
{
contains = FALSE;
}
return contains;
}
-(Rectangle *) intersect: (Rectangle *) rect
{
Rectangle *tempRect = [[Rectangle alloc] init];
XYPoint *tempPoint = [[XYPoint alloc] init];
[tempRect setWidth: 0 setHeight: 0];
[tempPoint setX: 0 setY: 0];
tempRect.origin = tempPoint;
ptA = self.origin;
[ptB setX: self.origin.xPoint setY: (self.origin.yPoint + self.height)];
[ptC setX: (self.origin.xPoint + self.width) setY: (self.origin.yPoint + self.height)];
[ptD setX: self.origin.xPoint setY: (self.origin.yPoint + self.height)];
ptE = rect.origin;
[ptF setX: rect.origin.xPoint setY: (rect.origin.yPoint + rect.height)];
[ptG setX: (rect.origin.xPoint + rect.width) setY: (rect.origin.yPoint + rect.height)];
[ptH setX: rect.origin.xPoint setY: (rect.origin.yPoint + rect.height)];
//
return tempRect;
}
@end
int main(void)
{
@autoreleasepool
{
Rectangle *rect1 = [[Rectangle alloc] init];
Rectangle *rect2 = [[Rectangle alloc] init];
Rectangle *newRect = [[Rectangle alloc] init];
XYPoint *point1 = [[XYPoint alloc] init];
XYPoint *point2 = [[XYPoint alloc] init];
[point1 setX: 200 setY: 420];
[point2 setX: 400 setY: 300];
[rect1 setWidth: 250 setHeight: 75];
[rect2 setWidth: 100 setHeight: 180];
rect1.origin = point1;
rect2.origin = point2;
newRect = [rect1 intersect: rect2];
}
return 0;
}