Member Avatar for Member #1083618

I **want to draw a magnifying glass using <graphics.h> ** in Turbo C++ (a DOS-based compiler for C++), which is mostly user as "Search" icon. But, I am having problems in making the rectangle attached to the two circles (one inscribed on another). I've also included the screenshot (made in PowerPoint) on how the output will look like, even

I've made most of the part, except the search icon (magnifying glass). How can I draw and colour it using floodfill(). Can you guys please help me?

The Link for the screenshot is :

Thanks, in advance! :)

Dani AI

Generated

For — a compact, reliable approach that works in Turbo C++/BGI: draw the rim and inner lens as two concentric circle outlines, draw the handle as a rotated rectangle (polygon) whose base starts on the rim, then use setfillstyle() + floodfill() to color the lens and the handle. Key points: 1) draw all outlines in the same border color (e.g. BLACK) before calling floodfill(), 2) make the handle polygon overlap the rim slightly (or start it from a computed point on the rim) so there are no 1-pixel gaps that let the fill leak, and 3) convert angles to radians for cos()/sin().

The snippet below shows the full idea: compute a start point on the circle at the desired angle, build four corner points for a rotated rectangle, draw the outlines with circle() and drawpoly(), then call floodfill() for the lens and for the handle. If floodfill() misbehaves, try using fillellipse() / fillpoly() instead — those avoid flood-fill boundary issues.

#include <graphics.h>
#include <conio.h>
#include <math.h>

int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); /* adjust path */

    int cx=200, cy=120, R=50, rim=6;
    int handle_len=80, handle_w=12;
    double ang = 45 * 3.14159265/180.0; /* 45 degrees */
    double dx = cos(ang), dy = sin(ang), nx = -dy, ny = dx;

    setcolor(BLACK);
    circle(cx,cy,R);           /* outer rim border */
    circle(cx,cy,R-rim);       /* inner border for lens */

    int sx = cx + (int)((R - rim/2.0) * dx);
    int sy = cy + (int)((R - rim/2.0) * dy);

    int p1x = sx + (int)(nx*(handle_w/2.0)), p1y = sy + (int)(ny*(handle_w/2.0));
    int p2x = sx - (int)(nx*(handle_w/2.0)), p2y = sy - (int)(ny*(handle_w/2.0));
    int p3x = p2x + (int)(dx*handle_len), p3y = p2y + (int)(dy*handle_len);
    int p4x = p1x + (int)(dx*handle_len), p4y = p1y + (int)(dy*handle_len);

    int pts[8] = {p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y};
    drawpoly(4, pts);

    setfillstyle(SOLID_FILL, LIGHTBLUE);
    floodfill(cx, cy, BLACK); /* fill lens */
    setfillstyle(SOLID_FILL, BROWN);
    floodfill((p1x+p3x)/2, (p1y+p3y)/2, BLACK); /* fill handle */

    /* optional highlight */
    setcolor(WHITE);
    fillellipse(cx - R/3, cy - R/3, R/8, R/12);

    getch();
    closegraph();
    return 0;
}

Notes: is right that graphics.h is DOS-era — if you can use a modern toolchain later, consider a WinBGIm port or an SDL/SFML approach. For this school assignment, the code above will get the exact “search” icon look: tweak R, rim, handle_len, handle_w, and ang to match your screenshot, and make the polygon overlap the rim by a pixel or two to guarantee a clean join.

Recommended Answers

All 2 Replies

I would suggest using something other than graphics.h. it is not supported by any modern compilers and it will only work in a dos enviroment.

Member Avatar for Member #1083618

But, I can't. I am preparing it for my high-school project.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.