Question

This is your last assignment for this course! It is about 3D vectors and arrays. You can either choose to work with vectors o
0.0) (199,199) (399,199) (599,199

Using C++ Language
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Summary :

Following are provided as part of the solution (i) notes , (ii) code & output

Notes :

Utilizing the array of pixels for 3 dimension , store the pixel data and following functions are implemented to achive the task.

fillPixels() - this method fills the rectangle area of 200x600 by white pixels

writePixels() this method takes the pixesl array and filename and writes the pixesl to file in ppm format.

drawRectangle() this method draws a rectangle of given width and height and given center x,y , in the pixel array and given rbg color.

drawL() this tries to draw a L shape given x1 , x2 and of width w and given color .

Utilizing above functions drew the given pattern as follow:

Divide the 200x600 into 3 compartments and in each comparment divide the height into 8 ( 25x8) and for eg S is achieved by drawing slabs of 3 horizontal bars and emebedded with square blocks of 25x25 each on either side .

############################### Code ###################

#include <iostream>
#include <fstream>

using namespace std;

#define HEIGHT 200
#define WIDTH 600
#define RGBNUM 3

// Draws a rectangle of given height , wdith and center x,y and with colors rgb
void drawRectangle(int pixel[HEIGHT][WIDTH][RGBNUM] , int x, int y , int ht , int wd , int cr , int cg , int cb)
{

        if ( (( y - ht/2 ) >= 0 ) & ( ( y + ht/2 ) <= HEIGHT ) & ( ( x - wd/2) >= 0  ) & ( (x + wd/2 ) <= WIDTH)) 
        {
                        
                for (int r = (y-ht/2) ; r <= ( y+ht/2 ) ; r++ )
                {
                        for (int c = (x-wd/2) ; c <= ( x+wd/2 ) ; c++ )
                        {
                                pixel[r][c][0] = cr ;
                                pixel[r][c][1] = cg ;
                                pixel[r][c][2] = cb ;
                        }
                }
                
        }
        else {
                cout << "  Rectangle  Out of range " <<  x << "  " << y << "\n";
        }
}

// fills the rectangle area with whilte pxels
void fillPixels ( int pixel[HEIGHT][WIDTH][RGBNUM] )
{
        for( int i=0 ; i < HEIGHT ; i++ )
        {
                for (int j=0 ; j< WIDTH ; j++ )
                {
                        pixel[i][j][0] = 255 ;
                        pixel[i][j][1] = 255 ;
                        pixel[i][j][2] = 255 ;
                }
        }
}

// Draws a L shape at given x1 to x2 of width and fill it with rgb 
void drawL(int pixel[HEIGHT][WIDTH][RGBNUM] , int x1, int x2, int y ,  int width , int height, int r , int g , int b)
{
        
        for( int i=0 ; i < width ; i++ )
        {
                int y1 = y ;
                for( int j = 0 ;  j < i+(x2-x1)/2  ; j++ )
                {
                        //cout << " X1 + j " << (x1 +  j )  << " x2 - j " << (x2- j ) << "\n";
                        pixel[y1][x1 + j - i][0] = r ; pixel[y1][x1+ j -i ][1] = g ; pixel[y1][x1+j-i][2] = b;
                        pixel[y1][x2 - j +i  ][0] = r ; pixel[y1][x2- j +i][1] = g ; pixel[y1][x2-j +i][2] = b;
                        
                        if ( (x1 + j - i ) == (x2 - j +i) ) 
                                break;
                        y1++;
                }
        }
}

void writeToFile(int pixel[HEIGHT][WIDTH][RGBNUM] , string filename )
{
        
         ofstream fhOut(filename);
          if (!fhOut.is_open()) {
                cout << "Cannot open output file : " <<   filename << "\n";
                return ;
        }
          
          fhOut << "P3\n"; 
          fhOut << WIDTH << " " <<  HEIGHT<< "\n";
          fhOut << 255 << "\n";
         
          for( int i=0 ; i < HEIGHT ; i++ )
        {
                for (int j=0 ; j< WIDTH ; j++ )
                {
                        fhOut << pixel[i][j][0] << " "
                                 << pixel[i][j][1]  << " "
                        << pixel[i][j][2]  << "  ";
                }
                fhOut << "\n";
        }
          
        fhOut.close();
}

int main()
{
        int ppm_pixels[200][600][3] ;
        
        fillPixels(ppm_pixels);
        // Vertical Bar Right
        //drawRectangle(ppm_pixels,150,100 , 20,30,34,35,150);
        
        //drawRectangle(ppm_pixels,75,75 , 100,15,30,50,150);
        // Vertical Bar left 
        //drawRectangle(ppm_pixels,175,75 , 100,15,30,50,150);
        
        // Base block 
        drawRectangle(ppm_pixels,100,138 , 25,100,30,50,150);
        
        drawRectangle(ppm_pixels,62.5,75 , 100 ,25,30,50,150);
        drawRectangle(ppm_pixels,138,75 , 100 ,25,30,50,150);
        
        //drawRectangle(ppm_pixels,300,150 , 25,100,30,50,150);
        
        // Draw middle S 
        drawRectangle(ppm_pixels,300,38 , 25,100,100,50,150);
        drawRectangle(ppm_pixels,262.5,63 , 25,25,100,50,150);
        
        drawRectangle(ppm_pixels,300,86 , 25,100,100,50,150);
        drawRectangle(ppm_pixels,338,111 , 25,25,100,50,150);

        drawRectangle(ppm_pixels,300,136 , 25,100,100,50,150);
        
        //drawRectangle(ppm_pixels,500,150 , 15,100,30,50,150);
        
        // Draw M 
        // vertical bars 2
        drawRectangle(ppm_pixels,450,85 , 125 ,25,30,150,150);
        drawRectangle(ppm_pixels,550,85 , 125 ,25,30,150,150);
        drawL(ppm_pixels,462.5,537.5,25, 25,575,30,150,150);
        writeToFile(ppm_pixels,"rct.ppm");
        
}

######################## Output #####################

USM 1100 1200 1300 400 1500 T.NO USM INOO MTTTT Imos ItoC

Add a comment
Know the answer?
Add Answer to:
Using C++ Language This is your last assignment for this course! It is about 3D vectors...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In t...

    c++ PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In this game, the user will need to match up the pairs symbols A,B,C,D,E on a 4x4 array. For example, the array could be initialized like the following: In this case, X represents an empty slot. The goal is for the user to match the A to the A, the B to the B, etc, until all pairs are matched up to win the...

  • Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows...

    Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows in a graphical way, as shown below, the names stored in a file. The coordinates to print the names will be also read from another file. Here is an example of what your graphic output should look like with the two files that are provided for test purposes. Drawing Panel Adam Bernie Cameron Daniel Fanny Gerard Harold Issac Jackie Raitlyn Note that the colors...

  • C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that...

    C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill have a private member variable for a dynamic array of doubles. It will also have two member variables of type...

  • in java Original: Sound Visualization Write a program that uses StdAudio and Picture to create an...

    in java Original: Sound Visualization Write a program that uses StdAudio and Picture to create an interesting two-dimensional color visualization of a sound file while it is playing. Be creative! Additional Notes: double[] data = StdAudio.read(<filename>); -> Takes a sound file and store it as a 2D array of real numbers. Picture pic = new Picture(300,200); -> Creates a picture 300 pixels wide and 200 pixels high Color c= new Color(<red>,<green>,<blue>); -> Each value of <red>, <green>, <blue> is between...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • C LANGUAGE!!! The program uses both files and two dimensional arrays. The Problem Statement Business is...

    C LANGUAGE!!! The program uses both files and two dimensional arrays. The Problem Statement Business is going well for your friend, who is selling discounts to area clubs and restaurants, which means that business is going well for you! Both of you have decided that you'll advertise on memory mall, which is roughly arranged as a rectangle. There's not enough time before a football game to hand flyers to everyone arranged on the mall. Therefore, you will only be able...

  • matlab 1. [suDmit as Iwo JPEGS in Canvas with your netiD in the title of each...

    matlab 1. [suDmit as Iwo JPEGS in Canvas with your netiD in the title of each plot] An electricity startup is looking into the viability of putting solar panels on the rooftops of buildings in Newark, NJ. They want you to help them come up with visualizations for data they have from the National Renewable Energy Laboratory and the National Oceanic and Atmospheric Administration about: - The average amount of sunshine received each month in the form of Global Horizonal...

  • I need help in C++ assignment. please add statements and i am Xcode complier user. Requested...

    I need help in C++ assignment. please add statements and i am Xcode complier user. Requested files: CountDecades.cpp (Download) Maximum upload file size: 96 KiB Write a C++ program named CountDecades.cpp. In this program you will have two integer arrays. One named inputArray of size 20 containing the values: 83, 2, 23, 11, 97, 23, 41, 67, 16, 25, 1 , 4, 75, 92, 52, 6, 44, 81, 8, 64 in the order specified, and an empty integer array of...

  • Write a program which will Ask the user a series of questions using the Scanner object Based on t...

    Write a program which will Ask the user a series of questions using the Scanner object Based on the input, draw a grid of star figures on the DrawingPanel You program should ask your user: . What R, G & B values to create a color to draw the figure? How many stars across should the fgure be? How many stars tall should the figure be? Note that your program does not need to error check the users responses Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT