Skip to content

Commit b98ea24

Browse files
Function to draw solid ellipses (Boronhub) (#111)
* draw ellipse * Update CAGGRenderer.cpp * Update CAGGRenderer.h * Add ellipse function to QuartzRenderer and change radius parameter name * bfix: fix ellipse function in SVG Renderer Co-authored-by: boron <[email protected]>
1 parent 403dfd5 commit b98ea24

File tree

8 files changed

+83
-1
lines changed

8 files changed

+83
-1
lines changed

Sources/AGGRenderer/AGGRenderer/AGGRenderer.swift

+16
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ public class AGGRenderer: Renderer{
123123
agg_object)
124124
}
125125

126+
public func drawSolidEllipse(center c: Point,
127+
radiusX rx: Float,
128+
radiusY ry: Float,
129+
fillColor: Color) {
130+
draw_solid_ellipse(c.x + xOffset,
131+
c.y + yOffset,
132+
rx,
133+
ry,
134+
fillColor.r,
135+
fillColor.g,
136+
fillColor.b,
137+
fillColor.a,
138+
agg_object)
139+
}
140+
141+
126142
public func drawSolidTriangle(point1: Point,
127143
point2: Point,
128144
point3: Point,

Sources/AGGRenderer/CAGGRenderer/CAGGRenderer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ void draw_solid_circle(float cx, float cy, float radius, float r, float g, float
2626
CPPAGGRenderer::draw_solid_circle(cx, cy, radius, r, g, b, a, object);
2727
}
2828

29+
void draw_solid_ellipse(float cx, float cy, float rx, float ry, float r, float g, float b, float a, const void *object){
30+
CPPAGGRenderer::draw_solid_ellipse(cx, cy, rx, ry, r, g, b, a, object);
31+
}
32+
2933
void draw_solid_triangle(float x1, float x2, float x3, float y1, float y2, float y3, float r, float g, float b, float a, const void *object){
3034
CPPAGGRenderer::draw_solid_triangle(x1, x2, x3, y1, y2, y3, r, g, b, a, object);
3135
}

Sources/AGGRenderer/CAGGRenderer/include/CAGGRenderer.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ void draw_solid_rect_with_border(const float *x, const float *y, float thickness
1616

1717
void draw_solid_circle(float cx, float cy, float radius, float r, float g, float b, float a, const void *object);
1818

19+
void draw_solid_ellipse(float cx, float cy, float rx, float ry, float r, float g, float b, float a, const void *object);
20+
1921
void draw_solid_triangle(float x1, float x2, float x3, float y1, float y2, float y3, float r, float g, float b, float a, const void *object);
2022

2123
void draw_solid_polygon(const float* x, const float* y, int count, float r, float g, float b, float a, const void *object);

Sources/AGGRenderer/CPPAGGRenderer/CPPAGGRenderer.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,21 @@ namespace CPPAGGRenderer{
324324
agg::render_scanlines(m_ras, m_sl_p8, ren_aa);
325325
}
326326

327+
void draw_solid_ellipse(float cx, float cy, float rx, float ry, float r, float g, float b, float a) {
328+
agg::rendering_buffer rbuf = agg::rendering_buffer(buffer, frame_width, frame_height, -frame_width*3);
329+
pixfmt pixf = pixfmt(rbuf);
330+
renderer_base rb = renderer_base(pixf);
331+
ren_aa = renderer_aa(rb);
332+
agg::ellipse ellipse(cx, cy, rx, ry, 100);
333+
Color c(r, g, b, a);
334+
agg::trans_affine matrix;
335+
matrix *= agg::trans_affine_translation(0, 0);
336+
agg::conv_transform<agg::ellipse, agg::trans_affine> trans(ellipse, matrix);
337+
m_ras.add_path(trans);
338+
ren_aa.color(c);
339+
agg::render_scanlines(m_ras, m_sl_p8, ren_aa);
340+
}
341+
327342
void draw_solid_triangle(float x1, float x2, float x3, float y1, float y2, float y3, float r, float g, float b, float a) {
328343
agg::rendering_buffer rbuf = agg::rendering_buffer(buffer, frame_width, frame_height, -frame_width*3);
329344
pixfmt pixf = pixfmt(rbuf);
@@ -539,6 +554,11 @@ namespace CPPAGGRenderer{
539554
plot -> draw_solid_circle(cx, cy, radius, r, g, b, a);
540555
}
541556

557+
void draw_solid_ellipse(float cx, float cy, float rx, float ry, float r, float g, float b, float a, const void *object){
558+
Plot *plot = (Plot *)object;
559+
plot -> draw_solid_ellipse(cx, cy, rx, ry, r, g, b, a);
560+
}
561+
542562
void draw_solid_triangle(float x1, float x2, float x3, float y1, float y2, float y3, float r, float g, float b, float a, const void *object){
543563
Plot *plot = (Plot *)object;
544564
plot -> draw_solid_triangle(x1, x2, x3, y1, y2, y3, r, g, b, a);

Sources/AGGRenderer/CPPAGGRenderer/include/CPPAGGRenderer.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace CPPAGGRenderer{
1414

1515
void draw_solid_circle(float cx, float cy, float radius, float r, float g, float b, float a, const void *object);
1616

17+
void draw_solid_ellipse(float cx, float cy, float rx, float ry, float r, float g, float b, float a, const void *object);
18+
1719
void draw_solid_triangle(float x1, float x2, float x3, float y1, float y2, float y3, float r, float g, float b, float a, const void *object);
1820

1921
void draw_solid_polygon(const float* x, const float* y, int count, float r, float g, float b, float a, const void *object);

Sources/QuartzRenderer/QuartzRenderer.swift

+12
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,18 @@ public class QuartzRenderer: Renderer {
348348
context.addEllipse(in: rectBound)
349349
context.drawPath(using: .fill)
350350
}
351+
352+
public func drawSolidEllipse(center c: Point,
353+
radiusX rx: Float,
354+
radiusY ry: Float,
355+
fillColor: Color) {
356+
let ellipse = CGMutablePath()
357+
ellipse.addEllipse(in: CGRect(x: CGFloat(c.x-rx), y: CGFloat(c.y-ry), width: CGFloat(rx*2), height: CGFloat(ry*2)),
358+
transform: CGAffineTransform(translationX: CGFloat(xOffset), y: CGFloat(yOffset)))
359+
context.setFillColor(fillColor.cgColor)
360+
context.addPath(ellipse)
361+
context.fillPath()
362+
}
351363

352364
public func drawSolidTriangle(point1: Point,
353365
point2: Point,

Sources/SVGRenderer/SVGRenderer.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class SVGRenderer: Renderer{
6969
lines.append(rectStr)
7070
drawHatchingRect(rect, hatchPattern: hatchPattern)
7171
}
72-
72+
7373
func drawHatchingRect(_ rect: Rect,
7474
hatchPattern: BarGraphSeriesOptions.Hatching) {
7575
let patternName: String
@@ -146,6 +146,15 @@ public class SVGRenderer: Renderer{
146146
lines.append(circle)
147147
}
148148

149+
public func drawSolidEllipse(center c: Point,
150+
radiusX rx: Float,
151+
radiusY ry: Float,
152+
fillColor: Color) {
153+
let c = convertToSVGCoordinates(c)
154+
let circle: String = #"<ellipse cx="\#(c.x)" cy="\#(c.y)" rx="\#(rx)" ry="\#(ry)" style="fill:\#(fillColor.svgColorString);opacity:\#(fillColor.a)" />"#
155+
lines.append(circle)
156+
}
157+
149158
public func drawSolidTriangle(point1: Point,
150159
point2: Point,
151160
point3: Point,

Sources/SwiftPlot/Renderer.swift

+17
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ public protocol Renderer: AnyObject{
129129
radius r: Float,
130130
fillColor: Color)
131131

132+
133+
/*drawSolidEllipse()
134+
*params: center c: Point,
135+
* radius rx: Float,
136+
* radius rx: Float,
137+
* fillColor: Color,
138+
* isOriginShifted: Bool
139+
*description: Draws an ellipse with specified fill color, center and radii
140+
* This function can operate in both coordinate systems with and
141+
* without shifted origin.
142+
* This is decided by the boolean parameter isOriginShifted.
143+
*/
144+
func drawSolidEllipse(center c: Point,
145+
radiusX rx: Float,
146+
radiusY ry: Float,
147+
fillColor: Color)
148+
132149
/*drawSolidTriangle()
133150
*params: point1: Point,
134151
* point2: Point,

0 commit comments

Comments
 (0)