Skip to content

Commit

Permalink
Directly use dynamic arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
vtereshkov committed Jan 9, 2022
1 parent 78e7d35 commit f5922f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
39 changes: 19 additions & 20 deletions umplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ typedef struct

typedef struct
{
Point *points;
int64_t numPoints;
UmkaDynArray(Point) points;
char *name;
Style style;
int64_t reserved;
} Series;


Expand Down Expand Up @@ -63,8 +63,7 @@ typedef struct

typedef struct
{
Series *series;
int64_t numSeries;
UmkaDynArray(Series) series;
Grid grid;
Titles titles;
Legend legend;
Expand Down Expand Up @@ -93,9 +92,9 @@ static Rectangle getLegendRect(const Plot *plot)
if (!plot->legend.visible)
return legendRect;

for (int iSeries = 0; iSeries < plot->numSeries; iSeries++)
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
{
const int labelWidth = MeasureText(plot->series[iSeries].name, plot->grid.fontSize);
const int labelWidth = MeasureText(plot->series.data[iSeries].name, plot->grid.fontSize);
if (labelWidth > legendRect.width)
legendRect.width = labelWidth;
}
Expand Down Expand Up @@ -156,12 +155,12 @@ static void resetTransform(const Plot *plot, ScreenTransform *transform)
Point minPt = (Point){ DBL_MAX, DBL_MAX};
Point maxPt = (Point){-DBL_MAX, -DBL_MAX};

for (int iSeries = 0; iSeries < plot->numSeries; iSeries++)
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
{
Series *series = &plot->series[iSeries];
for (int iPt = 0; iPt < series->numPoints; iPt++)
Series *series = &plot->series.data[iSeries];
for (int iPt = 0; iPt < series->points.len; iPt++)
{
const Point *pt = &series->points[iPt];
const Point *pt = &series->points.data[iPt];
if (pt->x > maxPt.x) maxPt.x = pt->x;
if (pt->x < minPt.x) minPt.x = pt->x;
if (pt->y > maxPt.y) maxPt.y = pt->y;
Expand Down Expand Up @@ -209,20 +208,20 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
Rectangle clientRect = getClientRect(plot);
BeginScissorMode(clientRect.x, clientRect.y, clientRect.width, clientRect.height);

for (int iSeries = 0; iSeries < plot->numSeries; iSeries++)
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
{
Series *series = &plot->series[iSeries];
Series *series = &plot->series.data[iSeries];
switch (series->style.kind)
{
case STYLE_LINE:
{
if (series->numPoints > 1)
if (series->points.len > 1)
{
Vector2 prevPt = getScreenPoint(series->points[0], transform);
Vector2 prevPt = getScreenPoint(series->points.data[0], transform);

for (int iPt = 1; iPt < series->numPoints; iPt++)
for (int iPt = 1; iPt < series->points.len; iPt++)
{
Vector2 pt = getScreenPoint(series->points[iPt], transform);
Vector2 pt = getScreenPoint(series->points.data[iPt], transform);
DrawLineEx(prevPt, pt, series->style.width, *(Color *)&series->style.color);
prevPt = pt;
}
Expand All @@ -232,9 +231,9 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)

case STYLE_SCATTER:
{
for (int iPt = 0; iPt < series->numPoints; iPt++)
for (int iPt = 0; iPt < series->points.len; iPt++)
{
Vector2 pt = getScreenPoint(series->points[iPt], transform);
Vector2 pt = getScreenPoint(series->points.data[iPt], transform);
DrawCircleV(pt, series->style.width, *(Color *)&series->style.color);
}
break;
Expand Down Expand Up @@ -371,9 +370,9 @@ static void drawLegend(const Plot *plot, const Font *font)

const Rectangle legendRect = getLegendRect(plot);

for (int iSeries = 0; iSeries < plot->numSeries; iSeries++)
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
{
Series *series = &plot->series[iSeries];
Series *series = &plot->series.data[iSeries];

// Legend mark
switch (series->style.kind)
Expand Down
26 changes: 3 additions & 23 deletions umplot.um
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ type (
reserved: int
}

SeriesImpl = struct {
points: ^Point
numPoints: int
name: str
style: Style
}

Grid* = struct {
xNumLines, yNumLines: int
color: uint32
Expand All @@ -52,14 +45,6 @@ type (
titles: Titles
legend: Legend
}

PlotImpl = struct {
series: ^SeriesImpl
numSeries: int
grid: Grid
titles: Titles
legend: Legend
}
)

fn (s: ^Series) clear*() {
Expand Down Expand Up @@ -122,19 +107,14 @@ fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {
return plt
}

fn umplot_plot(p: ^PlotImpl): int
fn umplot_plot(p: ^Plot): int

fn (p: ^Plot) plot*() {
seriesImpl := make([]SeriesImpl, len(p.series))

for i := 0; i < len(p.series); i++ {
s := &p.series[i]
s.trim()
seriesImpl[i] = SeriesImpl{&s.points[0], len(s.points), s.name, s.style}
p.series[i].trim()
}

plotImpl := PlotImpl{&seriesImpl[0], len(seriesImpl), p.grid, p.titles, p.legend}
umplot_plot(&plotImpl)
umplot_plot(p)
}


0 comments on commit f5922f5

Please sign in to comment.