Skip to content

Commit 57e7ccd

Browse files
committed
feat: PlotPolygon
1 parent 93c801b commit 57e7ccd

3 files changed

Lines changed: 115 additions & 5 deletions

File tree

implot.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ typedef int ImPlotItemFlags; // -> ImPlotItemFlags_
9595
typedef int ImPlotLineFlags; // -> ImPlotLineFlags_
9696
typedef int ImPlotScatterFlags; // -> ImPlotScatterFlags
9797
typedef int ImPlotBubblesFlags; // -> ImPlotBubblesFlags
98+
typedef int ImPlotPolygonFlags; // -> ImPlotPolygonFlags_
9899
typedef int ImPlotStairsFlags; // -> ImPlotStairsFlags_
99100
typedef int ImPlotShadedFlags; // -> ImPlotShadedFlags_
100101
typedef int ImPlotBarsFlags; // -> ImPlotBarsFlags_
@@ -271,6 +272,12 @@ enum ImPlotBubblesFlags_ {
271272
ImPlotBubblesFlags_None = 0, // default
272273
};
273274

275+
// Flags for PlotPolygon. Used by setting ImPlotSpec::Flags.
276+
enum ImPlotPolygonFlags_ {
277+
ImPlotPolygonFlags_None = 0, // default (closed, convex polygon)
278+
ImPlotPolygonFlags_Concave = 1 << 10, // use concave polygon filling (slower but supports concave shapes)
279+
};
280+
274281
// Flags for PlotStairs. Used by setting ImPlotSpec::Flags.
275282
enum ImPlotStairsFlags_ {
276283
ImPlotStairsFlags_None = 0, // default
@@ -960,6 +967,9 @@ IMPLOT_API void PlotScatterG(const char* label_id, ImPlotGetter getter, void* da
960967
IMPLOT_TMP void PlotBubbles(const char* label_id, const T* values, const T* szs, int count, double xscale=1, double xstart=0, const ImPlotSpec& spec=ImPlotSpec());
961968
IMPLOT_TMP void PlotBubbles(const char* label_id, const T* xs, const T* ys, const T* szs, int count, const ImPlotSpec& spec=ImPlotSpec());
962969

970+
// Plots a polygon. Points are specified as separate x and y arrays. Supports both convex and concave polygons.
971+
IMPLOT_TMP void PlotPolygon(const char* label_id, const T* xs, const T* ys, int count, const ImPlotSpec& spec=ImPlotSpec());
972+
963973
// Plots a a stairstep graph. The y value is continued constantly to the right from every x position, i.e. the interval [x[i], x[i+1]) has the value y[i]
964974
IMPLOT_TMP void PlotStairs(const char* label_id, const T* values, int count, double xscale=1, double xstart=0, const ImPlotSpec& spec=ImPlotSpec());
965975
IMPLOT_TMP void PlotStairs(const char* label_id, const T* xs, const T* ys, int count, const ImPlotSpec& spec=ImPlotSpec());
@@ -1193,18 +1203,18 @@ IMPLOT_API void PushStyleVar(ImPlotStyleVar idx, int val);
11931203
IMPLOT_API void PushStyleVar(ImPlotStyleVar idx, const ImVec2& val);
11941204
// Undo temporary style variable modification(s). Undo multiple pushes at once by increasing count.
11951205
IMPLOT_API void PopStyleVar(int count = 1);
1196-
1206+
11971207
// Gets the last item primary color (i.e. its legend icon color)
11981208
IMPLOT_API ImVec4 GetLastItemColor();
11991209

12001210
// Returns the null terminated string name for an ImPlotCol.
12011211
IMPLOT_API const char* GetStyleColorName(ImPlotCol idx);
12021212
// Returns the null terminated string name for an ImPlotMarker.
12031213
IMPLOT_API const char* GetMarkerName(ImPlotMarker idx);
1204-
1214+
12051215
// Returns the next marker and advances the marker for the current plot. You need to call this between Begin/EndPlot!
12061216
IMPLOT_API ImPlotMarker NextMarker();
1207-
1217+
12081218
//-----------------------------------------------------------------------------
12091219
// [SECTION] Colormaps
12101220
//-----------------------------------------------------------------------------
@@ -1274,7 +1284,7 @@ IMPLOT_API void BustColorCache(const char* plot_title_id = nullptr);
12741284
//-----------------------------------------------------------------------------
12751285
// [SECTION] Input Mapping
12761286
//-----------------------------------------------------------------------------
1277-
1287+
12781288
// Provides access to input mapping structure for permanent modifications to controls for pan, select, etc.
12791289
IMPLOT_API ImPlotInputMap& GetInputMap();
12801290

implot_demo.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,48 @@ void Demo_BubblePlots() {
445445

446446
//-----------------------------------------------------------------------------
447447

448+
void Demo_PolygonPlots() {
449+
// Triangle (convex)
450+
static float tri_xs[3] = {0.5f, 1.0f, 0.0f};
451+
static float tri_ys[3] = {1.0f, 0.0f, 0.0f};
452+
453+
// Pentagon (convex)
454+
static float pent_xs[5], pent_ys[5];
455+
for (int i = 0; i < 5; ++i) {
456+
float angle = (float)i * 2.0f * 3.14159f / 5.0f - 3.14159f / 2.0f;
457+
pent_xs[i] = 3.0f + 0.8f * cosf(angle);
458+
pent_ys[i] = 0.5f + 0.8f * sinf(angle);
459+
}
460+
461+
// Star (concave), counter-clockwise
462+
static float star_xs[10], star_ys[10];
463+
for (int i = 0; i < 10; ++i) {
464+
float angle = (float)i * 2.0f * 3.14159f / 10.0f - 3.14159f / 2.0f;
465+
float radius = (i % 2 == 0) ? 0.8f : 0.3f;
466+
star_xs[i] = 5.5f + radius * cosf(angle);
467+
star_ys[i] = 0.5f + radius * sinf(angle);
468+
}
469+
470+
if (ImPlot::BeginPlot("Polygon Plot", ImVec2(-1,0), ImPlotFlags_Equal)) {
471+
ImPlot::PlotPolygon("Triangle", tri_xs, tri_ys, 3, {
472+
ImPlotProp_FillAlpha, 0.5f,
473+
});
474+
ImPlot::PlotPolygon("Pentagon", pent_xs, pent_ys, 5, {
475+
ImPlotProp_FillAlpha, 0.5f,
476+
ImPlotProp_FillColor, ImVec4(0,1,0,1),
477+
});
478+
ImPlot::PlotPolygon("Star (Concave)", star_xs, star_ys, 10, {
479+
ImPlotProp_FillAlpha, 0.5f,
480+
ImPlotProp_FillColor, ImVec4(1,1,0,1),
481+
ImPlotProp_Flags, ImPlotPolygonFlags_Concave,
482+
});
483+
484+
ImPlot::EndPlot();
485+
}
486+
}
487+
488+
//-----------------------------------------------------------------------------
489+
448490
void Demo_StairstepPlots() {
449491
static float ys1[21], ys2[21];
450492
for (int i = 0; i < 21; ++i) {
@@ -2380,6 +2422,7 @@ void ShowDemoWindow(bool* p_open) {
23802422
DemoHeader("Shaded Plots##", Demo_ShadedPlots);
23812423
DemoHeader("Scatter Plots", Demo_ScatterPlots);
23822424
DemoHeader("Bubble Plots", Demo_BubblePlots);
2425+
DemoHeader("Polygon Plots", Demo_PolygonPlots);
23832426
DemoHeader("Realtime Plots", Demo_RealtimePlots);
23842427
DemoHeader("Stairstep Plots", Demo_StairstepPlots);
23852428
DemoHeader("Bar Plots", Demo_BarPlots);

implot_items.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,6 @@ struct RendererCircleLine : RendererBase {
17011701
mutable ImVec2 UV1;
17021702
};
17031703

1704-
17051704
static const ImVec2 MARKER_FILL_CIRCLE[10] = {ImVec2(1.0f, 0.0f), ImVec2(0.809017f, 0.58778524f),ImVec2(0.30901697f, 0.95105654f),ImVec2(-0.30901703f, 0.9510565f),ImVec2(-0.80901706f, 0.5877852f),ImVec2(-1.0f, 0.0f),ImVec2(-0.80901694f, -0.58778536f),ImVec2(-0.3090171f, -0.9510565f),ImVec2(0.30901712f, -0.9510565f),ImVec2(0.80901694f, -0.5877853f)};
17061705
static const ImVec2 MARKER_FILL_SQUARE[4] = {ImVec2(SQRT_1_2,SQRT_1_2), ImVec2(SQRT_1_2,-SQRT_1_2), ImVec2(-SQRT_1_2,-SQRT_1_2), ImVec2(-SQRT_1_2,SQRT_1_2)};
17071706
static const ImVec2 MARKER_FILL_DIAMOND[4] = {ImVec2(1, 0), ImVec2(0, -1), ImVec2(-1, 0), ImVec2(0, 1)};
@@ -1941,6 +1940,64 @@ void PlotBubbles(const char* label_id, const T* xs, const T* ys, const T* szs, i
19411940
CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
19421941
#undef INSTANTIATE_MACRO
19431942

1943+
//-----------------------------------------------------------------------------
1944+
// [SECTION] PlotPolygon
1945+
//-----------------------------------------------------------------------------
1946+
1947+
template <typename Getter>
1948+
void PlotPolygonEx(const char* label_id, const Getter& getter, const ImPlotSpec& spec) {
1949+
if (BeginItemEx(label_id, Fitter1<Getter>(getter), spec, spec.FillColor, spec.Marker)) {
1950+
if (getter.Count < 2) {
1951+
EndItem();
1952+
return;
1953+
}
1954+
const ImPlotNextItemData& s = GetItemData();
1955+
const bool is_concave = ImHasFlag(spec.Flags, ImPlotPolygonFlags_Concave);
1956+
1957+
ImDrawList& draw_list = *GetPlotDrawList();
1958+
const ImPlotAxis& x_axis = GetCurrentPlot()->Axes[GetCurrentPlot()->CurrentX];
1959+
const ImPlotAxis& y_axis = GetCurrentPlot()->Axes[GetCurrentPlot()->CurrentY];
1960+
Transformer2 transformer(x_axis, y_axis);
1961+
1962+
// Flip points to make sure they are in clockwise order for correct filling when one axis is inverted
1963+
bool x_inv = ImHasFlag(x_axis.Flags, ImPlotAxisFlags_Invert);
1964+
bool y_inv = ImHasFlag(y_axis.Flags, ImPlotAxisFlags_Invert);
1965+
bool flip = !((x_inv ? 1 : 0) ^ (y_inv ? 1 : 0));
1966+
1967+
// Transform all points to screen space
1968+
ImVec2* points = (ImVec2*)alloca(getter.Count * sizeof(ImVec2));
1969+
for (int i = 0; i < getter.Count; ++i) {
1970+
ImPlotPoint p = flip ? getter[getter.Count - 1 - i] : getter[i];
1971+
points[i] = transformer(p);
1972+
}
1973+
1974+
if (s.RenderFill && getter.Count >= 3) {
1975+
const ImU32 col_fill = ImGui::GetColorU32(s.Spec.FillColor);
1976+
if (is_concave)
1977+
draw_list.AddConcavePolyFilled(points, getter.Count, col_fill);
1978+
else
1979+
draw_list.AddConvexPolyFilled(points, getter.Count, col_fill);
1980+
}
1981+
if (s.RenderLine && getter.Count >= 2) {
1982+
const ImU32 col_line = ImGui::GetColorU32(s.Spec.LineColor);
1983+
draw_list.AddPolyline(points, getter.Count, col_line, ImDrawFlags_Closed, s.Spec.LineWeight);
1984+
}
1985+
1986+
EndItem();
1987+
}
1988+
}
1989+
1990+
template <typename T>
1991+
void PlotPolygon(const char* label_id, const T* xs, const T* ys, int count, const ImPlotSpec& spec) {
1992+
GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter(IndexerIdx<T>(xs,count,spec.Offset,Stride<T>(spec)),IndexerIdx<T>(ys,count,spec.Offset,Stride<T>(spec)),count);
1993+
return PlotPolygonEx(label_id, getter, spec);
1994+
}
1995+
1996+
#define INSTANTIATE_MACRO(T) \
1997+
template IMPLOT_API void PlotPolygon<T>(const char* label_id, const T* xs, const T* ys, int count, const ImPlotSpec& spec);
1998+
CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
1999+
#undef INSTANTIATE_MACRO
2000+
19442001
//-----------------------------------------------------------------------------
19452002
// [SECTION] PlotStairs
19462003
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)