/
PHLineWithCartesianEquation.m
152 lines (131 loc) · 3.01 KB
/
PHLineWithCartesianEquation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// PHLineWithCartesianEquation.m
// PHGraph
//
// Created by Pierre-Henri Jondot on 12/04/08.
// Ported to iPhone by brian@fluidmedia.com 08-2008
//
#import "PHLineWithCartesianEquation.h"
//#import "PHColor.h"
@implementation PHLineWithCartesianEquation
-(id)initWithXAxis:(PHxAxis*)aPHxAxis yAxis:(PHyAxis*)aPHyAxis a:(double)aValue b:(double)bValue
c:(double)cValue
{
[(PHGraphObject*)super initWithXAxis:aPHxAxis yAxis:aPHyAxis];
a = aValue;
b = bValue;
c = cValue;
style = PHStraight;
width = 1;
cocoaColor = [[UIColor blueColor] retain];
return self;
}
-(void)dealloc
{
[cocoaColor release];
[super dealloc];
}
-(void)setColor:(UIColor*)newColor
{
[newColor retain];
[cocoaColor release];
cocoaColor = newColor;
}
-(void)setEquationA:(double)aValue b:(double)bValue c:(double)cValue
{
a = aValue;
b = bValue;
c = cValue;
}
-(void)setWidth:(float)newWidth
{
width = newWidth;
}
-(void)setStyle:(int)newStyle
{
style = newStyle;
}
-(void)drawWithContext:(CGContextRef)context rect:(NSRect)rect
{
CGColorRef color = cocoaColor.CGColor;
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineWidth(context, width);
double xmin = [xAxis minimum];
double xmax = [xAxis maximum];
double ymin = [yAxis minimum];
double ymax = [yAxis maximum];
if ([xAxis style] & PHIsLog)
{
xmin = pow(10,xmin);
xmax = pow(10,xmax);
}
if ([yAxis style] & PHIsLog)
{
ymin = pow(10,ymin);
ymax = pow(10,ymax);
}
CGContextBeginPath(context);
switch (style) {
case PHDashed33:
{
float dash[2] = {3,3};
CGContextSetLineDash(context,0,dash,2);
}
break;
case PHDashed8212:
{
float dash[4] = {8,2,1,2};
CGContextSetLineDash(context,0,dash,4);
}
break;
}
BOOL intersectBorder = NO;
if ((a*xmin+b*ymin+c)*(a*xmin+b*ymax+c)<0)
{
intersectBorder = YES;
float x = rect.origin.x;
float y = (rect.size.height+rect.origin.y)-(a*xmin+b*ymin+c)/(b*(ymin-ymax))*rect.size.height;
CGContextMoveToPoint(context, x, y);
}
if ((a*xmin+b*ymax+c)*(a*xmax+b*ymax+c)<0)
{
float x = rect.origin.x + (a*xmin+b*ymax+c)/(a*(xmin-xmax))*rect.size.width;
float y = (rect.size.height+rect.origin.y)-rect.size.height;
if (intersectBorder)
{
CGContextAddLineToPoint(context, x, y);
CGContextStrokePath(context);
return;
} else
{
intersectBorder = YES;
CGContextMoveToPoint(context, x, y);
}
}
if ((a*xmax+b*ymax+c)*(a*xmax+b*ymin+c)<0)
{
float x = rect.origin.x+rect.size.width;
float y = (rect.size.height+rect.origin.y)-(a*xmax+b*ymin+c)/(b*(ymin-ymax))*rect.size.height;
if (intersectBorder)
{
CGContextAddLineToPoint(context, x, y);
CGContextStrokePath(context);
return;
} else
{
intersectBorder = YES;
CGContextMoveToPoint(context, x, y);
}
}
if ((a*xmax+b*ymin+c)*(a*xmin+b*ymin+c)<0)
{
float x = rect.origin.x+(a*xmin+b*ymin+c)/(a*(xmin-xmax))*rect.size.width;
float y = (rect.size.height+rect.origin.y);
if (intersectBorder)
{
CGContextAddLineToPoint(context, x, y);
CGContextStrokePath(context);
}
}
}
@end