Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:33:51+00:00 2026-06-12T11:33:51+00:00

I was trying to draw a shaded triangle, but I get wrong colors. Here’s

  • 0

I was trying to draw a shaded triangle, but I get wrong colors. Here’s the example of what I get, if I call a function with red, green and blue colors:

enter image description here

It should be red on the top left, green on the top right and blue on the top bottom. But as you can see the colors are not like that. I should get something like this:

enter image description here

Here’s the code:

unsigned RGB(unsigned char R, unsigned char G, unsigned B){
    return  (R << 16) | (G << 8) | B;
}

    float get_dist(int x1, int y1, int x2, int y2){
            int xproj = x2-x1;
            int yproj = y2-y1;
            float dist = sqrt(pow(xproj, 2) + pow(yproj, 2));
            return dist;
    }
    unsigned char getR(unsigned c){
            return (c >> 16) & 0xFF;
    }
    unsigned char getG(unsigned c){
            return (c >> 8) & 0xFF;
    }
    unsigned char getB(unsigned c){
            return c & 0xFF;
    }
    void draw_ftriangle(SDL_Surface * s, int x0, int y0, unsigned c0, int x1, int y1, unsigned c1, int x2, int y2, unsigned c2){
            int x;
            signed d0, d1, d2;
            unsigned R, G, B, color;
            int ymin = min(y0, min(y1, y2));
            int xmin = min(x0, min(x1, x2));
            int ymax = max(y0, max(y1, y2));
            int xmax = max(x0, max(x1, x2));
            draw_line(s, x0, y0, x1, y1, color);
            draw_line(s, x1, y1, x2, y2, color);
            draw_line(s, x0, y0, x2, y2, color);
            for(; ymin < ymax; ymin++)
                    for(x=xmin; x < xmax; x++)
                            if(ptintr(x, ymin, x0, y0, x1, y1, x2, y2)){
                                    d0 = get_dist(x, ymin, x0, y0);
                                    d1 = get_dist(x, ymin, x1, y1);
                                    d2 = get_dist(x, ymin, x2, y2);
                                    R = d0+d1+d2 == 1 ? getR(c0)*d0 + getR(c1)*d1 + getR(c2)*d2 :
                                                                             (getR(c0)*d0 + getR(c1)*d1 + getR(c2)*d2)/(d0+d1+d2);
                                    G = d0+d1+d2 == 1 ? getG(c0)*d0 + getG(c1)*d1 + getG(c2)*d2 :
                                                                            (getG(c0)*d0 + getG(c1)*d1 + getG(c2)*d2)/(d0+d1+d2);
                                    B = d0+d1+d2 == 1 ? getB(c0)*d0 + getB(c1)*d1 + getB(c2)*d2 :
                                                                            (getB(c0)*d0 + getB(c1)*d1 + getB(c2)*d2)/(d0+d1+d2);
                                    color = RGB(R, G, B);
                                    put_pixel(s, x, ymin, color);
                            }
    }

I call a function drraw_ftriangle like that:

draw_ftriangle(surface, 100, 100, RGB(255, 0, 0),
235, 150, RGB(0, 255, 0),
4, 254, RGB(0, 0, 255));

I’m not sure, but I think the problem should be here:

R = (d0+d1+d2) == 1 ? getR(c0)*d0 + getR(c1)*d1 + getR(c2)*d2 :
                      (getR(c0)*d0 + getR(c1)*d1 + getR(c2)*d2)/(d0+d1+d2);

Same with G and B variables…

I’d like other people to explain me, where’s the problem, because I’m searching for it all day long…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:33:53+00:00Added an answer on June 12, 2026 at 11:33 am

    The R and G parameters to your RGB function are unsigned char, and you are not casting them to unsigned int before shifting. That means, you’re shifting an 8-bit number. You know what happens…

    So, either convert the parameters to unsigned int or cast the values before shifting.

    PS: Whatever you do, it would be nice if you made all three parameters the same type!

    As in comments, turns out it was this:

    So I don’t think your (x,y) coordinates are normalised in that
    get_dist function. You compute the length of the hypoteneuse
    connecting those two vectors, but you don’t give it any base lengths
    to normalise on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to draw a picture to the window but it is only
I'm trying to get MRT working in OpenGL to try out deferred rendering. Here's
I'm trying to draw a line with soft edges, regardless of the slope. Here's
I'm trying to get a handle on OpenGL VAOs/VBOs, and conceptually I'm there, but
I am trying to draw a line using the Graphics 2D but then the
I am trying to draw a cylinder, but its not working :( Can you
I'm trying to draw a polygon using c# and directx All I get is
I am trying to draw a waveform. I have a precalculated amount of datapoints
I am trying to draw a line along with mouse move on a paper.
I am trying to draw in a web worker using html5 canvas. The worker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.