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

  • Home
  • SEARCH
  • 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 8681543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:32:19+00:00 2026-06-12T21:32:19+00:00

I am trying to generate 4 triangles based on user inputted parameters for a

  • 0

I am trying to generate 4 triangles based on user inputted parameters for a rectangle. My idea is to get the slope from the center of the rectangle to the origin, which on a ppm file is in the top left. This causes everything to be reversed essentially. Anyway, I believe the trouble I am having is from an embedded loop in my main function but I am not certain. My output is of 3 triangles where the triangle on the left overruns the bottom triangle as if it were constantly hugging the slope.

The output is piped to file.ppm and then viewed.

#include <stdio.h>
#include <math.h>

void make_header (int width, int height);
void make_pixel (unsigned char r, unsigned char g, unsigned char b);
void print_Row (float yValue, int width, int height);
void print_Row2 (float yValue, int width, int height);

void make_header(int width, int height)
{
    fprintf(stdout,"P6\n");
    fprintf(stdout,"%d %d 255\n",width,height);
}

void make_pixel (unsigned char r, unsigned char g, unsigned char b)
{
    fprintf(stdout,"%c%c%c", r,g,b);
}

void print_Row(float yValue, int width, int height)
{
    int x;

    for(x=0;x<yValue;x++)   
    {
        make_pixel(255,0,0); // left triangle
    }


    for(x=x; x<width-yValue; x++)
    {
        make_pixel(0,255,0); //top triangle
    }

    for(x=x;x<width;x++)
    {
        make_pixel(0,0,255);  // right triangle
    }
}   

void print_Row2(float yValue, int width, int height)
{
    int x;

    for(x=0;x<yValue;x++)
    {
        make_pixel(255,0,0);
    }

    for(x=x;x<width-yValue;x++)
    {
        make_pixel(0,0,0); //bottom triangle
    }

    for(x=x;x<width;x++)
    {
        make_pixel(0,0,255);
    }
}   

int main()
{
    float slope, inv_slope, width_midpoint, height_midpoint, yValue ;
    int width,height,x,b;

    fprintf(stderr, "\nEnter width: ");
    scanf("%d", &width);
    fprintf(stderr, "\nEnter height: ");
    scanf("%d", &height);
    make_header(width, height);

    width_midpoint = width / 2;
    height_midpoint = height / 2;
    slope = (height_midpoint -0) / (width_midpoint - 0);
    inv_slope = pow(slope,-1);


    for(b=0;b<height_midpoint;b++)
    {   
        for(x=0;x<width;x++)
        {
            yValue = (inv_slope)*(x) + (b);
            print_Row(yValue, width, height);
        }
    }

    for(b=height_midpoint; b<=height; b++)
    {
        for(x=0;x<width;x++)
        {
            yValue = (-1*(slope))*(x)+(b);
            print_Row2(yValue,width,height);
        }
    }

    return 0;
}
  • 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-12T21:32:20+00:00Added an answer on June 12, 2026 at 9:32 pm

    You are actually writing way too much data. Replace the main loops with this, and everything should be ok 😉

    for(b=0;b<height_midpoint;b++)
    {   
        for(x=0;x<width;x++)
        {
            if(x < (b / slope)) { 
                make_pixel(255,0,0);
            }
            else if( x > (width - (b / slope))) {
                make_pixel(0,255,0);
            }
            else
                make_pixel(0,0,255);
        }
    }
    
    for(b=height_midpoint; b<=height; b++)
    {
        for(x=0;x<width;x++)
        {
            if( x > (b / slope)) {
                make_pixel(0,255,0);
            }
            else if(x > (width - (b / slope))) { 
                make_pixel(0,0,0);
            }
            else
                make_pixel(255,0,0);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to generate db driven menu which based on parent->child structure. All root menu
I trying to generate an XML document which is around 23 to 30 MB,
Im am trying to generate interfaces of HP Quality Center OTACLien.dll using com4j. I
I'm trying to generate a list of events that a user is attending. All
I'm trying to generate SQL table header from a csv file. As I had
I'm trying to generate an XML output from SQL and need to use a
Trying to generate XML file from returend SQL query results. The SQL query returns
While trying to generate the wsdl for my services I get the message: Handler
while trying to generate a proxy from this WebService (http://scdemo14.infor.com:9014/axis/services/wb:wsclocks-inbound?wsdl) I'm receiving this error
I'm trying to generate a pdf file from data in a database using JasperReports

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.