I’m writing code using c++ with imgui dx9, I’m writing a graph as values are passed
I have following code:
void DrawHexagonWithAttributes(const ImVec2& center, float radius, ImU32 color, ImU32 border_color, float thickness, bool filled = true)
{
ImDrawList* draw_list = ImGui::GetWindowDrawList();
ImVec2 points[6];
const float rotation_angle = IM_PI / 2.0f;
for (int i = 0; i < 6; ++i)
{
float angle = IM_PI / 3.0f * i + rotation_angle;
points[i] = ImVec2(center.x + cosf(angle) * radius, center.y + sinf(angle) * radius);
}
if (filled)
draw_list->AddConvexPolyFilled(points, 6, color);
draw_list->AddPolyline(points, 6, border_color, true, thickness);
}
void RenderHexagonGraph(int var1, int var2, int var3, int var4, int var5, int var6)
{
ImVec2 window_pos = ImGui::GetWindowPos();
ImVec2 window_size = ImGui::GetWindowSize();
ImVec2 center = ImVec2(window_pos.x + window_size.x * 0.5f, window_pos.y + window_size.y * 0.5f);
const ImVec4 colors[4] = {
ImVec4(0.467f, 0.867f, 0.467f, 1.0f),
ImVec4(0.6f, 1.0f, 0.6f, 1.0f),
ImVec4(0.757f, 1.0f, 0.757f, 1.0f),
ImVec4(0.831f, 1.0f, 0.870f, 1.0f)
};
const float radii[4] = {
45.0f,
75.0f,
105.0f,
135.0f
};
const float border_thickness[4] = { 1.0f, 1.0f, 1.0f, 3.0f };
const ImU32 border_color = ImGui::ColorConvertFloat4ToU32(Ragnarok::getInstance().HexToImVec4(0xE0E0E0));
ImDrawList* draw_list = ImGui::GetWindowDrawList();
ImVec2 hex_points[4][6];
const float rotation_angle = IM_PI / 2.0f;
for (int i = 3; i >= 0; --i)
{
for (int j = 0; j < 6; ++j)
{
float angle = IM_PI / 3.0f * j + rotation_angle;
hex_points[i][j] = ImVec2(center.x + cosf(angle) * radii[i], center.y + sinf(angle) * radii[i]);
}
DrawHexagonWithAttributes(center, radii[i], ImGui::ColorConvertFloat4ToU32(colors[i]), border_color, border_thickness[i]);
}
for (int i = 0; i < 3; ++i)
{
int opposite_index = i + 3;
draw_list->AddLine(hex_points[3][i], hex_points[3][opposite_index], border_color, 1.0f);
}
ImVec2 graph_points[6];
int stats[6] = { var1, var2, var3, var4, var5, var6 };
int max_stat = *std::max_element(stats, stats + 6);
for (int i = 0; i < 6; ++i)
{
float angle = IM_PI / 3.0f * i + rotation_angle;
float value_ratio = static_cast<float>(stats[i]) / static_cast<float>(max_stat);
graph_points[i] = ImVec2(center.x + cosf(angle) * radii[3] * value_ratio, center.y + sinf(angle) * radii[3] * value_ratio);
}
ImU32 graph_color = ImGui::ColorConvertFloat4ToU32(ImVec4(1.0f, 0.412f, 0.380f, 0.8f));
draw_list->AddConvexPolyFilled(graph_points, 6, graph_color);
const char* labels[6] = { "VAR1", "VAR2", "VAR3", "VAR4", "VAR5", "VAR6" };
const ImU32 label_color = ImGui::ColorConvertFloat4ToU32(ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
for (int i = 0; i < 6; ++i)
{
float angle = IM_PI / 3.0f * i + rotation_angle;
ImVec2 text_pos = ImVec2(
center.x + cosf(angle) * (radii[3] + 15.0f),
center.y + sinf(angle) * (radii[3] + 15.0f)
);
if (i == 0)
{
text_pos.y -= 15.0f;
}
else if (i == 3)
{
text_pos.y += 15.0f;
}
else
{
text_pos.x += (cosf(angle) > 0 ? 15.0f : -15.0f);
}
ImVec2 text_size = ImGui::CalcTextSize(labels[i]);
draw_list->AddText(ImVec2(text_pos.x - text_size.x * 0.5f, text_pos.y - text_size.y * 0.5f), label_color, labels[i]);
}
const float line_thickness = 2.0f;
const ImU32 line_color = ImGui::ColorConvertFloat4ToU32(ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
}
I used the following method for testing:
int var1 = 1615;
int var2 = 200;
int var3 = 400;
int var4 = 1500;
int var5 = 600;
int var6 = 0;
RenderHexagonGraph(var1, var2, var3, var4, var5, var6);
but I have some problems
The first is to keep the names from var1 to var6 always at the ends 6px away from the tip of the hexagon
second is that the sequence must start at the top, from var1 going from the top to the right, becoming var1, var2 and so on until 6
And thirdly my graph is not working as expected, this is the result I am getting
The result I expect is like this