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 6350371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:47:54+00:00 2026-05-24T21:47:54+00:00

I am trying to implement a gradient brush from scratch in C++ with GDI.

  • 0

I am trying to implement a gradient brush from scratch in C++ with GDI. I don’t want to use GDI+ or any other graphics framework. I want the gradient to be of any direction (arbitrary angle).

My algorithm in pseudocode:

      For each pixel in x dirrection
      For each pixel in the y direction
          current position = current pixel - centre                      //translate origin 
          rotate this pixel according to the given angle
          scalingFactor =( rotated pixel + centre ) / extentDistance    //translate origin back 
          rgbColor = startColor + scalingFactor(endColor - startColor)

extentDistance is the length of the line passing from the centre of the rectangle and has gradient equal to the angle of the gradient

Ok so far so good. I can draw this and it looks nice. BUT unfortunately because of the rotation bit the rectangle corners have the wrong color. The result is perfect only for angle which are multiples of 90 degrees. The problem appears to be that the scaling factor doesn’t scale over the entire size of the rectangle.

I am not sure if you got my point cz it’s really hard to explain my problem without a visualisation of it.

If anyone can help or redirect me to some helpful material I’d be grateful.

  • 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-05-24T21:47:55+00:00Added an answer on May 24, 2026 at 9:47 pm

    Ok guys fixed it. Apparently the problem was that when I was rotating the gradient fill (not the rectangle) I wasn’t calculating the scaling factor correctly. The distance over which the gradient is scaled changes according to the gradient direction. What must be done is to find where the edge points of the rect end up after the rotation and based on that you can find the distance over which the gradient should be scaled. So basically what needs to be corrected in my algorithm is the extentDistance.

    How to do it:

    •Transform the coordinates of all four corners

    •Find the smallest of all four x’s as minX

    •Find the largest of all four x’s and call it maxX

    •Do the same for y’s.

    •The distance between these two point (max and min) is the extentDistance

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

Sidebar

Related Questions

I am trying implement Unblock me Puzzle. i want to change image position from
Am trying to implement a generic way for reading sections from a config file.
im trying to implement a custom error page, what i want to be able
Trying to implement the decorator pattern in C# from the code in the Head
trying to implement a multiplayer. Using the sample from Game Center - Sending and
Trying to implement the example from here. Facebook Share passes along the URL of
I'm trying to implement Stochastic gradient descent in MATLAB. I followed the algorithm exactly
I'm trying to implement the code from showing dialog while loading layout by setContentView
im trying to implement local search api, to retreive data for shops and other
I'm trying to implement a simple gradient boosting algorithm for regression in R. This

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.