-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathXN2DPlot.m
164 lines (124 loc) · 3.73 KB
/
XN2DPlot.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
153
154
155
156
157
158
159
160
161
162
163
164
//
// XN2DPlot.m
// XNMaths
//
// XN2DPlot class.
// Provides methods to plot within Objective-C object notation.
//
// Created by Нат Гаджибалаев on 23.11.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
//
// Import it's own header
#import "XN2DPlot.h"
//
// Import what was marked with @class
#import "XNFunction.h"
#import "XNLineData.h"
#import "XNFloatRange.h"
#import "XNPlotManager.h"
//
// Import PLPlot
#import "plplot.h"
@implementation XN2DPlot
#pragma mark -
#pragma mark Class init methods
+ (XN2DPlot*) plotInRect: (NSRect)rect label: (NSString*)newTitle quality: (NSUInteger)newQuality
{
return [[[XN2DPlot alloc] initInRect:rect label:newTitle quality:newQuality] autorelease];
}
- (XN2DPlot*) initInRect: (NSRect)rect label: (NSString*)newTitle quality: (NSUInteger)newQuality
{
// set range from rect
xRange = [XNFloatRange rangeWithMin: rect.origin.x max: (rect.origin.x + rect.size.width)];
yRange = [XNFloatRange rangeWithMin: rect.origin.y max: (rect.origin.y + rect.size.height)];
//NSLog(@"Will do plot in rect (%f, %f) to (%f, %f)", xRange.min, xRange.max, yRange.min, yRange.max);
// set quality
quality = newQuality;
// and set title at last.
label = [newTitle copy];
// connect to manager and start it.
if([[XNPlotManager sharedManager] addPlot]){
isReadyToRender = YES;
} else {
[NSException raise: @"XNPlotManager error."
format: @"XNPlotManager refused to register the plot. It servs %d plots already.", [XNPlotManager sharedManager].connectedPlots];
};
// now use plplot routines
// pladv(0);
// plvpor(0.05, 0.95, 0.05, 0.95);
// plwind(xRange.min, xRange.max, yRange.min, yRange.max);
// plbox("bn", 0, 0, "bn", 0, 0);
plenv(xRange.min, xRange.max, yRange.min, yRange.max, 0, 1);
pllab("(x axis)", "(y axis)", [label UTF8String]);
return self;
}
#pragma mark -
#pragma mark Drawing API
//
//// FUNCTION
//
- (void) renderFunction: (XNFunction*)aFunction range: (XNFloatRange*)range color: (NSColor*)color width: (NSUInteger)width
{
XNLineData *line = [aFunction createLineDataInRange: range withQuality:quality];
[self renderLine: line
color: color
width: width];
}
//
//// POINTS
//
- (void) renderPoint: (XN2DPoint)point color: (NSColor*) color
{
CGFloat* x = calloc(1, sizeof(CGFloat));
CGFloat* y = calloc(1, sizeof(CGFloat));
x[0] = point.x;
y[0] = point.y;
[self renderPointsWithX:x y:y count:1 color:color];
free(x);
free(y);
}
- (void) renderPoints: (NSArray*) arrayOfPoints color:(NSColor*) color
{
CGFloat* x = calloc(arrayOfPoints.count, sizeof(CGFloat));
CGFloat* y = calloc(arrayOfPoints.count, sizeof(CGFloat));
for( NSUInteger i = 0; i < arrayOfPoints.count; i++){
x[i] = [[arrayOfPoints objectAtIndex:i] pointValue].x;
y[i] = [[arrayOfPoints objectAtIndex:i] pointValue].y;
}
[self renderPointsWithX:x y:y count:arrayOfPoints.count color:color];
free(x);
free(y);
}
- (void) renderPointsWithX: (CGFloat*)x y: (CGFloat*)y count: (NSUInteger)count color: (NSColor*) color
{
plscol0(15, (NSInteger)([color redComponent]*255), (NSInteger)([color greenComponent]*255), (NSInteger)([color blueComponent]*255));
plcol(15);
plpoin(count, x, y, 21);
plcol(1);
}
//
//// LINE
//
- (void) renderLine: (XNLineData *)data color: (NSColor *)color width: (NSUInteger)width
{
// draw!
// pladv(screenNumber);
plscol0(15, (NSInteger)([color redComponent]*255), (NSInteger)([color greenComponent]*255), (NSInteger)([color blueComponent]*255));
plcol(15);
plwid(width);
plline(data.pointsCount, data.xData, data.yData);
// back to default width:
plwid(1);
plcol(1);
}
#pragma mark -
#pragma mark Dealloc
- (void) dealloc
{
[label release];
[xRange release];
[yRange release];
[super dealloc];
}
@end