CS50- PSet4 Filter-more - c

I cant find what is wrong with my code for blur function. The error states my code is unable to filter 3x3 image and 4x4 image correctly but it is able to filter middle, edge and corner pixel. Seeking help to understand what is wrong.
Below is my code for blur function:
`
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp[height][width];
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
temp[i][j]=image[i][j];
}
}
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
int counter;
float sum_red;
float sum_blue;
float sum_green;
sum_red=sum_blue=sum_green=counter=0;
for(int k=-1;k<2;k++)
{
for(int l=-1;l<2;l++)
{
if((i+k)<0 || (i+k)>=height)
{
continue;
}
if((j+l)<0 || (j+1)>=width)
{
continue;
}
sum_red+=temp[i+k][j+l].rgbtRed;
sum_blue+=temp[i+k][j+l].rgbtBlue;
sum_green+=temp[i+k][j+l].rgbtGreen;
counter++;
}
}
image[i][j].rgbtRed= round(sum_red/counter);
image[i][j].rgbtBlue= round(sum_blue/counter);
image[i][j].rgbtGreen= round(sum_green/counter);
}
}
return;
}
`

Related

How can I draw a spline curves with c in codeblocks?

I am trying to draw a minimum enclosing circle and I need spline curves for points. but also I shouldn't use libraries. For preview, I used lines but they need curves. How can I fix it?
Which one is true arc, line or points?
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <conio.h>
struct nokta
{
int x,y;
};
//-------------------------------------------------------------//
int main()
{
int gd = DETECT;
int gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
initwindow(800,800);
struct nokta noktalar[50];
FILE *noktaal=fopen("noktalar.txt","r");
int i=0;
int sayac=0;
if(noktaal==NULL)
{
printf("Dosya yok");
}
while(!feof(noktaal))
{
fscanf(noktaal,"%d %d",&noktalar[i].x,&noktalar[i].y);
sayac++;
i++;
};
for(int i=0; i<sayac-1; i++)
{
printf("%d %d \n",noktalar[i].x,noktalar[i].y);
}
fclose(noktaal);
//----------------------------------------------------------------//
if(sayac==1)//If there is no point
{
printf("Lutfen koordinat giriniz.");
}
if(sayac==2)// If there is one point
{
printf("Lutfen birden fazla koordinat giriniz.");
}
if(sayac==3)//If there is two point
{
int uzunluk1=abs(noktalar[1].x-noktalar[0].x);
int uzunluk2=abs(noktalar[1].y-noktalar[0].y);
double yaricap=sqrt(pow(uzunluk1,2)+pow(uzunluk2,2));
float merkezx=(noktalar[1].x+noktalar[0].x)/2.0;
float merkezy=(noktalar[1].y+noktalar[0].y)/2.0;
printf("Yaricap:%.2f\n",yaricap/2);
printf("Merkez noktasi:{%.2f,%.2f}",merkezx,merkezy);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());//y düzlemi
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);//x düzlemi
putpixel(((getmaxx()/2)+(noktalar[0].x*20)),(getmaxy()/2)-(noktalar[0].y*20),GREEN);
putpixel(((getmaxx()/2)+(noktalar[1].x*20)),(getmaxy()/2)-(noktalar[1].y*20),GREEN);
for(int i=20; i<=800; i=i+20)
{
line(i,getmaxy()/2.03,i,getmaxy()/1.97);
}
for(int i=20; i<=800; i=i+20)
{
line(getmaxx()/2.03,i,getmaxx()/1.97,i);
}
circle((getmaxx()/2)+(merkezx*20),(getmaxy()/2)-(merkezy*20),yaricap*7.9);
getch();
closegraph();
}
if(sayac>3)//If there is three or more points.
{
int karetoplam;
double enbuyuk=1.0,yaricap1;
float merkez1x,merkez1y;
int sayac1=0;
int ikinoktauzakligi=0;
for(int i=0; i<sayac-1; i++)
{
for(int j=0; j<sayac-1; j++)
{
float ykare=pow(abs(noktalar[i].y-noktalar[j].y),2);
float xkare=pow(abs(noktalar[i].x-noktalar[j].x),2);
karetoplam=ykare+xkare;
if(enbuyuk<=karetoplam)
{
enbuyuk=karetoplam;
}
if(enbuyuk==karetoplam)
{
merkez1x=(noktalar[i].x+noktalar[j].x)/2.0;
merkez1y=(noktalar[i].y+noktalar[j].y)/2.0;
++sayac1;
}
}
}
if(sayac1>=2)
{
double cap1=sqrt(enbuyuk);
yaricap1=cap1/2;
printf("Ilk yari capi:%.2f\n",yaricap1);
printf("Ilk merkez noktasi:{%.2f,%.2f}\n",merkez1x,merkez1y);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());//y düzlemi
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);//x düzlemi
for(int i=20; i<=800; i=i+20)
{
line(i,getmaxy()/2.03,i,getmaxy()/1.97);
}
for(int i=20; i<=800; i=i+20)
{
line(getmaxx()/2.03,i,getmaxx()/1.97,i);
}
for(int i=0; i<sayac-1; i++)
{
for(int j=0; j<sayac-1; j++)
{
putpixel((getmaxx()/2)+(noktalar[i].x*20),(getmaxy()/2)-(noktalar[i].y*20),GREEN);
}
}
float uzunluk2;
float arttir=0.0;
float enbuyuk2=0.0;
for(int i=0; i<100;i++)
{
for(int j=0; j<sayac-1; j++)
{
uzunluk2=sqrt((pow(noktalar[j].x,2)+pow(noktalar[j].y,2))-(pow(merkez1x,2)+pow(merkez1y,2)));
if(enbuyuk2<uzunluk2)
{
enbuyuk2=uzunluk2;
}
if(yaricap1<enbuyuk2)
{
yaricap1=yaricap1+0.1;
arttir=arttir+0.1;
}
}
}
printf("Son yari capi:%.2f\n",yaricap1);
printf("Son merkez noktasi:{%.2f,%.2f}",merkez1x+arttir,merkez1y+arttir);
int uzunluk3;
int enkucuk3=0;
for(i=0;i<sayac-1;i++)
{
line((getmaxx()/2)+(noktalar[i].x*20),((getmaxy()/2)+(noktalar[i].y*20)),((getmaxx()/2)+(noktalar[i+1].x*20)),((getmaxy()/2)+(noktalar[i+1].y*20)));
}
circle((getmaxx()/2)+((merkez1x+arttir)*20),(getmaxy()/2)-((merkez1y+arttir)*20),yaricap1*20);
getch();
closegraph();
}
}
}
//-------------------------------------------------------------------//
Minimum enclosing circle can be drawn with this but it doesn't show a curve. I need to show curves without libraries.

Printing patterns using arrays in c

The problem is to print this pattern:
5555555555
5444444445
5433333345
5432222345
5432112345
5432112345
5432222345
5433333345
5444444445
5555555555
This is my code:
#include<stdio.h>
int main()
{
int i,k,j,n,p;
printf("enter the no : ");
scanf("%d",&n);
p = n;
k = 0;
int a[2*n][2*n];
while (p>=1)
{
for(i=0+k;i<2*n-k;i++)
{
for(j=0+k;j<2*n-k;j++)
{
if(i == 2*n-k||i == k||j == k||j == 2*n-k)
{
a[i][j]=p;
}
else
{
a[i][j]= 8;
}
}
printf("\n");
}
k++,p--;
}
for(i=0;i<2*n;i++)
{
for(j=0;j<2*n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
The result I get is:
5555555555
5444444448
5433333388
5432222888
5432118888
5432188888
5432888888
5438888888
5488888888
5888888888
Your code is overly complex. You should utilize the symmetry. Replace you while-loop with this code.
// Because of symmetry, both i and j can loop to n instead of 2*n
for(i=0; i<n; i++) {
p = n;
for(j=0; j<n; j++) {
// Assign four cells at once due to both horizontal and vertical symmetry
a[i][j] = a[i][2*n-j-1] = a[2*n-i-1][j] = a[2*n-i-1][2*n-j-1] = p;
if(j<i)
p--;
}
}

Can't swap top element with bottom element in matrix

I'm trying to write sliding puzzle where I'm using 3x3 matrix as board. In main function user inputs the number of tile which swaps positions with 0. Everything works except when I enter number that is located in a position above zero it does not swap positions with it. How do I correct that?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#define empty_space 0
int m,n,i,j,z,y;
void print_matrix(int matrix[3][3])
{
for ( m=0; m<3; m++){
for (n=0; n<3; n++)
{
printf("%d\t",matrix[m][n]);
} printf("\n");
}
printf("\n");
}
void swap(int *i, int *j) {
int t = *i;
*i = *j;
*j = t;
}
void slide(int a[3][3] , int t)
{
for ( i=0; i<3; i++){
for ( j=0; j<3; j++)
{
if (a[i][j]==t)
{
if (a[i+1][j]==empty_space && i+1<=2)
{
swap(&a[i+1][j],&a[i][j]);break;
}
if (a[i-1][j]==empty_space && i-1>=0)
{
swap(&a[i][j],&a[i-1][j]);break;
}
if (a[i][j+1]==empty_space && j+1<=2)
{
swap(&a[i][j],&a[i][j+1]);break;
}
if (a[i][j-1]==empty_space && j-1>=0)
{
swap(&a[i][j],&a[i][j-1]);break;
}
}
}
}
}
int goal_test (int a[3][3],int b[3][3])
{
int flag=0;
for ( z=0; z<3; z++){
for ( y=0; y<3; y++){
if(a[z][y]==b[z][y])
flag++;
}
}
if (flag==9)
return 1;
else return 0;
}
int main()
{
static int mat[3][3]={{1,2,3},{6,0,4},{7,5,8}};
int goal[3][3]={{1,0,3},{6,5,4},{7,8,2}};
print_matrix(mat);
int x;
while(goal_test(mat,goal)==0)
{
printf("enter tile to slide:\t");
scanf("%d",&x);
slide(mat, x);
print_matrix(mat);
}
return 0;
}
Here's what happens:
Any number can swap its position as long as it's not located above zero

Change fill color of cell when clicked in Processing

I've managed to print the co-ordinates of a cell when it's clicked.
Now I'm looking for help and advice to use the this data in order to change the cell colour to red - unless the cell selected is a base cell, I want those to remain as a constant. Here's the code I'm working with:
Tile[][] tileArray = new Tile[10][10];
int rows = 10;
int cols = 10;
int[] base = { 4,4, 4,5, 4,6, 4,7 };
void setup() {
size(500, 500);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
tileArray[i][j] = new Tile(i*50, j*50);
}
}
}
void draw() {
for (int i=0; i<base.length; i+=2) {
fill(0, 0, 255);
rect(base[i]*50, base[i+1]*50, 50, 50);
}
}
void mousePressed() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
println (mouseX/50 +"," + mouseY/50);
}
}
}
class Tile {
Tile(int x, int y) {
fill(255);
stroke(0);
rect(x, y, 50, 50);
}
}
EDIT:
I've added the functionality which I am looking for, but I assume that I have not stored my base cell data correctly. As you can still click on the base cell and an active cell is drawing underneath. Here is the new code:
Tile[][] tiles;
int gridSize = 10;
int tileSize = 50;
void setup() {
size (450, 400);
generateGrid();
}
void draw() {
background (255);
display();
}
public void generateGrid() {
tiles = new Tile[gridSize][gridSize];
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
tiles[i][j] = new Tile((i*50), (j*50), tileSize, tileSize);
}
}
}
public void display() {
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
tiles[i][j].display();
}
}
}
void mousePressed() {
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
println (mouseX/50 +"," + mouseY/50);
}
}
int mx = mouseX/50;
int my = mouseY/50;
tiles[mx][my].active = !tiles[mx][my].active;
// tiles[mx][my].base = !tiles[mx][my].base;
println(tiles[mx][my].active);
}
class Tile {
int tx, ty, tw, th;
int[] baseCell = { 4, 2, 4, 3, 4, 4, 4, 5 };
color col_default = color(255);
color col_base = color(0, 0, 255);
color col_active = color(255, 100, 50);
boolean active = false;
boolean base = false;
Tile (int itx, int ity, int itw, int ith) {
tx = itx;
ty = ity;
tw = itw;
th = ith;
}
void display() {
stroke(0);
fill(col_default);
if ( active ) fill (col_active);
rect(tx, ty, tw, th);
for (int i=0; i<baseCell.length; i+=2) {
fill(col_base);
rect(baseCell[i]*50, baseCell[i+1]*50, 50, 50);
}
}
}
Your Tile class doesn't really make sense right now. It doesn't really make sense to draw to the screen from a constructor. Instead, move that logic into a draw() function inside the Tile class.
Your Tile class also has to keep track of its state: whether it's a base cell or not, or any other kind of cell it can be. You might do this with an enum, or an int, or a set of booleans. That part is completely up to you.
Then in your Tile.draw() function, you should check that state to decide what color to draw. From the sketch's draw() function, simply loop over each Tile and tell it to draw itself.
Finally, when you click, simply set the state of the Tile that you clicked on.

Not sure what's wrong with my logic for my snake game?

For reference, here is the link to my assignment (the course is a C language course): Snake
The mouth is supposed to be "<", ">", "v", or "^", depending on the direction, and the body segments are supposed to be "*".
So first of all, my up and down arrow key controls are switched, but it prints the correct character (i.e. it prints "v" when moving down and "^" when moving up), and I cannot figure out why.
Also, as the head moves, it just detaches from the body and doesn't print new body segments (See Screencap).
Below are my custom structs, main, scene_render and scene_update functions (we are NOT allowed to modify the main function for this assignment):
struct Point{
int x;
int y;
};
struct Snake {
struct Point segments[MAX_SEGMENTS];
int num_segments;
int dir; //0=up, 1=down, 2=left, 3=right
};
struct Scene {
// TODO: add fields
struct Snake snake;
struct Point fruit;
};
main
int main(void) {
// Important: do NOT modify the main function
struct Scene scene;
scene_init(&scene);
int keep_going = 1;
while (keep_going == 1) {
scene_render(&scene);
cons_update();
scene_delay(&scene);
keep_going = scene_update(&scene);
}
return 0;
}
scene_render
void scene_render(const struct Scene *s) {
// TODO: add your code
int num=s->snake.num_segments;
cons_clear_screen();
cons_move_cursor(s->snake.segments[0].y, s->snake.segments[0].x);
if(s->snake.dir==0)
{
cons_printw("v");
}
else if(s->snake.dir==1)
{
cons_printw("^");
}
else if(s->snake.dir==2)
{
cons_printw(">");
}
else
{
cons_printw("<");
}
for(int i=1; i <num; i++)
{
cons_move_cursor(s->snake.segments[i].y, s->snake.segments[i].x);
cons_printw("*");
}
cons_move_cursor(23, 79); //just to get blinking cursor out of the way
}
scene_update
int scene_update(struct Scene *s) {
// This function should return 0 if the player presses 'q',
// 1 otherwise.
struct Scene temp=*s;
int num=temp.snake.num_segments;
int key = cons_get_keypress();
// TODO: add your code
if(key=='q')
{
return 0;
}
if(key==UP_ARROW && temp.snake.dir!=1)
{
temp.snake.dir=0;
temp.snake.segments[0].y=(temp.snake.segments[0].y)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(key==DOWN_ARROW && temp.snake.dir!=0)
{
temp.snake.dir=1;
temp.snake.segments[0].y=(temp.snake.segments[0].y)-1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(key==LEFT_ARROW && temp.snake.dir!=3)
{
temp.snake.dir=2;
temp.snake.segments[0].x=(temp.snake.segments[0].x)-1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(key==RIGHT_ARROW && temp.snake.dir!=2)
{
temp.snake.dir=3;
temp.snake.segments[0].x=(temp.snake.segments[0].x)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(key==-1)
{
if(temp.snake.dir==0)
{
temp.snake.segments[0].y=(temp.snake.segments[0].y)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(temp.snake.dir==1)
{
temp.snake.segments[0].y=(temp.snake.segments[0].y)-1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(temp.snake.dir==2)
{
temp.snake.segments[0].x=(temp.snake.segments[0].x)-1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else
{
temp.snake.segments[0].x=(temp.snake.segments[0].x)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
}
temp.fruit.x=s->fruit.x;
temp.fruit.y=s->fruit.y;
*s=temp;
return 1;
}
The reason why the body is detached from the head is because your movement code:
if(temp.snake.dir==0)
{
temp.snake.segments[0].y=(temp.snake.segments[0].y)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
should be:
if(temp.snake.dir==0)
{
temp.snake.segments[0].y=(temp.snake.segments[0].y)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i-1].y;
temp.snake.segments[i].x=s->snake.segments[i-1].x;
}
}
In other words, the first body part should move to where the head used to be. The second body part should move to where the first part used to be, etc. The way you have it right now, all the body parts just stay in the same place all the time. You need to make this change in many places, or better yet make a function to move the body so you don't have duplicated code everywhere.
As for why up is down and vice versa, I can only imagine that the y coordinates of your screen are opposite what you think they are (increasing y values moves up vs down). You should check which way is which and make sure your code matches.

Resources