Skip to content

Instantly share code, notes, and snippets.

@rokups
Last active August 17, 2022 12:22
Show Gist options
  • Save rokups/38c4e33791bf7fa28060d6842d3ab443 to your computer and use it in GitHub Desktop.
Save rokups/38c4e33791bf7fa28060d6842d3ab443 to your computer and use it in GitHub Desktop.
static inline float ImCross(const ImVec2& p0, const ImVec2& p1) { return p0.x * p1.y - p0.y * p1.x; }
// Returns +1 if c is on the left side and -1 if it is on the right side of line (a, b).
#define ImOrient(a, b, c) (ImCross((b) - (a), (c) - (a)) > 0 ? +1 : -1)
bool ImQuadContainsPoint(ImVec2 p1, ImVec2 p2, ImVec2 p3, ImVec2 p4, ImVec2 pt)
{
if (ImAbs(ImOrient(p1, p2, pt) + ImOrient(p2, p3, pt) + ImOrient(p3, p1, pt)) == 3) // Is inside triangle (p1, p2, p3)
return true;
if (ImAbs(ImOrient(p1, p3, pt) + ImOrient(p3, p4, pt) + ImOrient(p4, p1, pt)) == 3) // Is inside triangle (p1, p3, p4)
return true;
return false;
}
#undef ImOrient
ImGuiContext& g = *GImGui;
ImDrawList* draw_list = ImGui::GetForegroundDrawList();
ImVec2 p1(100.0f + 16.0f * ImSin(g.Time * 5.0f), 100.0f + 11.0f * ImCos(g.Time * 7.0f));
ImVec2 p2(100.0f + 11.0f * ImSin(g.Time * 3.0f), 200.0f + 16.0f * ImCos(g.Time * 5.0f));
ImVec2 p3(200.0f + 16.0f * ImSin(g.Time * 5.0f), 200.0f + 11.0f * ImCos(g.Time * 3.0f));
ImVec2 p4(200.0f + 11.0f * ImSin(g.Time * 7.0f), 100.0f + 16.0f * ImCos(g.Time * 5.0f));
draw_list->AddCircleFilled(p1, 2.0f, IM_COL32_WHITE);
draw_list->AddCircleFilled(p2, 2.0f, IM_COL32_WHITE);
draw_list->AddCircleFilled(p3, 2.0f, IM_COL32_WHITE);
draw_list->AddCircleFilled(p4, 2.0f, IM_COL32_WHITE);
if (ImQuadContainsPoint(p1, p2, p3, p4, g.IO.MousePos))
draw_list->AddQuadFilled(p1, p2, p3, p4, IM_COL32(0, 255, 0, 255));
else
draw_list->AddQuad(p1, p2, p3, p4, IM_COL32(255, 0, 0, 255));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment