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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:09:04+00:00 2026-05-30T15:09:04+00:00

I am trying to replace a part of an image with a rotated version

  • 0

I am trying to replace a part of an image with a rotated version of another one. The image source should appear in such a way that origin_source ends up at orign_dest in image *this. Also source should be rotated around origin_source before replacing pixels.

The code below works when R is a mirror matrix, but if I actually makes it to a rotation matrix, the resulting image becomes sheared. What is wrong?

void Image::imageApply(const Image& source,const Point& origin_dest,const Point& origin_source,const Point& direction)
{
Matrix22<double> R=transformRotationCreate(direction);
Point source_new_size=sqrt(2)*((Point){source.widthGet(),source.heightGet()});
Point blit_start=origin_dest;
for(unsigned int k=0;k<source_new_size.y;k++)
    {
    for(unsigned int l=0;l<source_new_size.x;l++)
        {
        Point point_source=(Point){l,k};
        Point point_dest=point_source-origin_source;
        point_dest*=R;
        point_dest+=blit_start;

        if(point_source.rectangleInIs((Point){0,0},(Point){source.widthGet(),source.heightGet()})
            &&point_dest.rectangleInIs((Point){0,0},(Point){widthGet(),heightGet()}))
            {
            (*this)(point_dest)=source(point_source);
            }
        }
    }
}

Here are some other functions used:

T=double

template<class T>
struct Matrix22
{
T xx;
T xy;
T yx;
T yy;
};

direction is a normalized vector

inline Matrix22<double> transformRotationCreate(const Vector2d<double>& direction)
{
return (Matrix22<double>){direction.x, -direction.y, direction.y, direction.x};
}

Also

Vector2d<T>& operator*=(const Matrix22<T>& M)
    {
    x=x*M.xx + y*M.xy;
    y=x*M.yx + y*M.yy;
    return *this;
    }
  • 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-30T15:09:05+00:00Added an answer on May 30, 2026 at 3:09 pm

    I solved it

    First of all, the matrix-vector multiplication operator was wrong:

    Vector2d<T>& operator*=(const Matrix22<T>& M)
        {
        T x_old=x;  //Need to save old x value (Stupid error but anyone does so sometimes)
        x=x*M.xx + y*M.xy;
        y=x_old*M.yx + y*M.yy;
        return *this;
        }
    

    The final “rotate-and-paste” routine looks like this:

    void Image::imageApply(const Image& source,const Point& origin_dest,const Point& origin_source,const Point& direction)
    {
    Matrix22<double> R=transformRotationCreate(direction);
    Point blit_start=origin_dest-(Point){source.sizeMaxGet(),source.sizeMaxGet()};
    Point blit_end=origin_dest+(Point){source.sizeMaxGet(),source.sizeMaxGet()};
    
    for(unsigned int k=blit_start.y;k<blit_end.y;k++)
        {
        for(unsigned int l=blit_start.x;l<blit_end.x;l++)
            {
            Point point_dest=(Point){l,k};
            Point point_source=R*(point_dest - origin_dest) + origin_source;
            if(point_source.rectangleInIs((Point){0,0},(Point){source.widthGet(),source.heightGet()} ))
                {
                float alpha_source=source(point_source).alpha;
                (*this)(point_dest)=(1.0f-alpha_source)*(*this)(point_dest)
                                    + alpha_source*source(point_source);
                }
            }
        }
    }
    

    Finally, the rotation direction was wrong, but that is just a swap of the xy factors in the transformation.

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

Sidebar

Related Questions

I am trying to replace an image when a link in another part of
So I'm trying to replace a part of a texture over another in GLSL,
i have an input soap message, trying to replace part of namespace uri with
I'm trying to replace pieces of XML and need an accumulator along the way.
I am trying to replace $1, $2, $3 variables in a URL with another
I'm trying to replace a small part of text in a large HTML document
I am actually trying to replace a part of the string in a text
I am trying to populate a WebGrid cell with an image that will open
I'm trying to get an image to display with one of the colors replaced
I'm trying to replace a part of a string. The part contains some special

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.