Create a particle circle - c

I'm creating a particles engine in C (with the CSFML library) where each particle has a position on {X,Y}, a life, and a movement vector in {X,Y}.
I'm actually creating X particles at the same position on my screen (ex: {100, 100}, and i wan't to make them moving to create a growing circle from the initial position.
What is the mathematical function that can help me to make this ?
#include <SFML/Graphics/Vertex.h>
#include <math.h>
#include "main.h"
int my_rand(int a, int b){
return rand()%(b-a) +a;
}
partBuffer *newPartBuffer(int size)
{
partBuffer *this;
const size_t size_m = (sizeof(partBuffer) + sizeof(sfVertex) * size * 4
+ sizeof(t_info) * size);
void *ptn = malloc(size_m);
if (ptn == NULL)
return (NULL);
memset(ptn, 0, size_m);
this = (partBuffer*)(ptn);
this->size = size;
this->vertex = (sfVertex*)(ptn + sizeof(partBuffer));
this->info = (t_info*)(this->vertex + (size * 4));
return (this);
}
void setPart(partBuffer *this, uint id, sfVector2f pos)
{
if (id >= this->size)
return ;
this->vertex[(id * 4) + 0].position = (sfVector2f){pos.x + 0, pos.y + 0};
this->vertex[(id * 4) + 1].position = (sfVector2f){pos.x + 5, pos.y + 0};
this->vertex[(id * 4) + 2].position = (sfVector2f){pos.x + 5, pos.y + 5};
this->vertex[(id * 4) + 3].position = (sfVector2f){pos.x + 0, pos.y + 5};
this->vertex[(id * 4) + 0].color = (sfColor){255, 255, 255};
this->vertex[(id * 4) + 1].color = (sfColor){255, 255, 255};
this->vertex[(id * 4) + 2].color = (sfColor){255, 255, 255};
this->vertex[(id * 4) + 3].color = (sfColor){255, 255, 255};
//this->info[id].speed = (sfVector2f){my_rand(-3, 3), my_rand(-3, 3)};
this->info[id].speed = (sfVector2f){0, 0};
this->info[id].life = 1.0;
}
static uint newPart(partBuffer *this)
{
for (uint id = this->size - 1; id != 0; id -= 1)
if (this->info[id].life <= 0)
return (id);
return ((uint)(-1));
}
void updatePartBuffer(partBuffer *this)
{
for (uint id = 0; id < this->size; id += 1) {
if (this->info[id].life > 0)
this->info[id].life -= 0.0;
if (this->info[id].life <= 0)
this->info[id].life = 0.0;
this->vertex[(id * 4) + 0].position.x += this->info[id].speed.x;
this->vertex[(id * 4) + 1].position.x += this->info[id].speed.x;
this->vertex[(id * 4) + 2].position.x += this->info[id].speed.x;
this->vertex[(id * 4) + 3].position.x += this->info[id].speed.x;
this->vertex[(id * 4) + 0].position.y += this->info[id].speed.y;
this->vertex[(id * 4) + 1].position.y += this->info[id].speed.y;
this->vertex[(id * 4) + 2].position.y += this->info[id].speed.y;
this->vertex[(id * 4) + 3].position.y += this->info[id].speed.y;
this->vertex[(id * 4) + 0].color.a = (sfUint8)(this->info[id].life * 255.);
this->vertex[(id * 4) + 1].color.a = (sfUint8)(this->info[id].life * 255.);
this->vertex[(id * 4) + 2].color.a = (sfUint8)(this->info[id].life * 255.);
this->vertex[(id * 4) + 3].color.a = (sfUint8)(this->info[id].life * 255.);
}
}
void drawPartBuffer(partBuffer *this, sfRenderWindow *window)
{
sfRenderWindow_drawPrimitives(window, this->vertex, this->size * 4,
sfQuads, NULL);
}
void set_circle(partBuffer *this, float points)
{
sfVector2f start = {0, -1};
sfVector2f adder = {1 / (points), 1 / (points)};
for (uint id = 0; id < points; id += 1) {
this->info[id].speed = (sfVector2f){start.x + adder.x * id, start.y + adder.y * id};
}
}
int game_loop(sfRenderWindow *window)
{
partBuffer *buffer = newPartBuffer(10000);
int points = 11;
sfClock *clock = sfClock_create();
sfVector2f position = {250, 250};
for (uint nb = 0; nb < points; nb += 1)
setPart(buffer, nb, position);
set_circle(buffer, points);
while (sfRenderWindow_isOpen(window)) {
if (sfTime_asMilliseconds(sfClock_getElapsedTime(clock)) >= 10) {
updatePartBuffer(buffer);
sfClock_restart(clock);
}
sfRenderWindow_clear(window, sfBlack);
drawPartBuffer(buffer, window);
sfRenderWindow_display(window);
}
}
int main(void)
{
sfRenderWindow *window = sfRenderWindow_create((sfVideoMode){500, 500, 32},
"Title", sfDefaultStyle, NULL);
sfRenderWindow_setFramerateLimit(window, 120);
game_loop(window);
return (0);
}

Related

So, this function was supposed to use edge detection and apply sobel filter. But well It doesn't

I'm trying to figure out what I did wrong. My intent is in title :)
This is the code I tried, and it clearly isn't working. It just print's 1 solid colour, also edges are not included yet. What part is wrong I can't figure it out what part of this code is wrong. It's just a function and it's in C.
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE edit[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
edit[i][j] = image[i][j];
}
}
for (int q = 1; q < height - 1; q++)
{
for (int w = 1; w < width - 1; w++)
{
int redEdit[9];
int greenEdit[9];
int blueEdit[9];
for (int top = 0; top < 3; top++)
{
redEdit[top] += edit[q - 1][w - 1 + top].rgbtRed;
greenEdit[top] += edit[q - 1][w - 1 + top].rgbtGreen;
blueEdit[top] += edit[q - 1][w - 1 + top].rgbtBlue;
}
int midctr = 0;
for (int mid = 3; mid < 6; mid++)
{
redEdit[mid] += edit[q][w - 1 + midctr].rgbtRed;
greenEdit[mid] += edit[q][w - 1 + midctr].rgbtGreen;
blueEdit[mid] += edit[q][w - 1 + midctr].rgbtBlue;
midctr ++;
}
int topctr = 0;
for (int top = 6; top < 9; top++)
{
redEdit[top] += edit[q + 1][w - 1 + topctr].rgbtRed;
greenEdit[top] += edit[q + 1][w - 1 + topctr].rgbtGreen;
blueEdit[top] += edit[q + 1][w - 1 + topctr].rgbtBlue;
topctr ++;
}
int matrixgx[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
int matrixgy[] = {- 1, -2, -1, 0, 0, 0, 1, 2, 1};
int redEditgx[9];
int greenEditgx[9];
int blueEditgx[9];
int redEditgy[9];
int greenEditgy[9];
int blueEditgy[9];
for (int mtrx = 0; mtrx < 9; mtrx++)
{
redEditgx[mtrx] = redEdit[mtrx] * matrixgx[mtrx];
greenEditgx[mtrx] = greenEdit[mtrx] * matrixgx[mtrx];
blueEditgx[mtrx] = blueEdit[mtrx] * matrixgx[mtrx];
redEditgy[mtrx] = redEdit[mtrx] * matrixgy[mtrx];
greenEditgy[mtrx] = greenEdit[mtrx] * matrixgy[mtrx];
blueEditgy[mtrx] = blueEdit[mtrx] * matrixgy[mtrx];
}
// now sum up the changes of gx and gt
int redSumgx = 0;
int greenSumgx = 0;
int blueSumgx = 0;
int redSumgy = 0;
int greenSumgy = 0;
int blueSumgy = 0;
for (int sum = 0; sum < 9; sum++)
{
redSumgx += redEditgx[sum];
greenSumgx += greenEditgx[sum];
blueSumgx += blueEditgx[sum];
redSumgy += redEditgy[sum];
greenSumgy += greenEditgy[sum];
blueSumgy += blueEditgy[sum];
}
int finalRed = round(sqrt(pow(redSumgx, 2) + pow(redSumgy, 2)));
int finalGreen = round(sqrt(pow(greenSumgx, 2) + pow(greenSumgy, 2)));
int finalBlue = round(sqrt(pow(blueSumgx, 2) + pow(blueSumgy, 2)));
if (finalRed > 255)
{
finalRed = 255;
}
if (finalGreen > 255)
{
finalGreen = 255;
}
if (finalBlue > 255)
{
finalBlue = 255;
}
image[q][w].rgbtRed = finalRed;
image[q][w].rgbtGreen = finalGreen;
image[q][w].rgbtBlue = finalBlue;
}
}
return;
}
I know this code is cancer please don't judge
These arrays
int redEdit[9];
int greenEdit[9];
int blueEdit[9];
have not been initialised and hold arbitrary values. So when you execute statements such as
redEdit[top] += edit[q - 1][w - 1 + top].rgbtRed;
the sum is meaningless. You need
int redEdit[9] = { 0 };
int greenEdit[9] = { 0 };
int blueEdit[9] = { 0 };
Only static variables are implicitly initialised to 0. Local (auto) variables are not.

cell overlap removal in vlsi placement

Below code is to remove overlap among cells residing inside grids.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
//coordinate representation
struct Point
{
double x;
double y;
};
//block representation with 4 coordinates
struct Block
{
unsigned int id;
struct Point bottomLeft, topLeft, bottomRight, topRight;
double width, height;
double whitespace;
};
struct Overlap
{
double overlapLength;
struct Block *b1, *b2;
} OL[1000];
struct Block *b;
struct Block ROW1, ROW2, ROW3;
int comparator(const void *p, const void *q)
{
return (((struct Overlap *)p)->overlapLength -
(((struct Overlap *)q)->overlapLength));
}
//intialise Blocks using bottom left x,y coordinate
void intialiseBlock(struct Block *B)
{
B->topLeft.x = B->bottomLeft.x;
B->topLeft.y = B->height + B->bottomLeft.y;
B->bottomRight.x = B->width + B->bottomLeft.x;
B->bottomRight.y = B->bottomLeft.y;
B->topRight.x = B->width + B->bottomLeft.x;
B->topRight.y = B->height + B->bottomLeft.y;
}
//update coordinate
void move(double xDelta, double yDelta, struct Point *p)
{
// Parameter values are increments to the current coordinates
p->x += xDelta;
p->y += yDelta;
}
//calculate and return distance between 2 points
double distance(const struct Point aPoint, const struct Point bPoint)
{
return sqrt((aPoint.x - bPoint.x) * (aPoint.x - bPoint.x) +
(aPoint.y - bPoint.y) * (aPoint.y - bPoint.y));
}
//generate width of cell using number of cells
double computeCellWidth(int ncell)
{
double width = (0.8 * 400) / ncell;
return width;
}
//create a
unsigned int rand_interval(unsigned int min, unsigned int max)
{
int r;
const unsigned int range = 1 + max - min;
const unsigned int buckets = RAND_MAX / range;
const unsigned int limit = buckets * range;
/* Create equal size buckets all in a row, then fire randomly towards
* the buckets until you land in one of them. All buckets are equally
* likely. If you land off the end of the line of buckets, try again. */
do
{
r = rand();
} while (r >= limit);
return min + (r / buckets);
}
//place blocks in appropriate rows
void placeVertical(struct Block *b, int n, struct Block ROW1, struct Block ROW2,
struct Block ROW3)
{
for (int i = 0; i < n; i++)
{
double distanceFromBottom = 0.0;
double distanceFromTop = 0.0;
// check if Inside Row1 and Row2
if ((b[i].bottomLeft.y < ROW1.topLeft.y) && (b[i].topLeft.y > ROW1.topLeft.y))
{
//printf("ROW1 and ROW2 Executed\n");
distanceFromBottom = ROW1.topLeft.y - b[i].bottomLeft.y;
distanceFromTop = b[i].height - distanceFromBottom;
if (distanceFromBottom > distanceFromTop)
{
move(0, -distanceFromTop, &b[i].bottomLeft);
move(0, -distanceFromTop, &b[i].topLeft);
move(0, -distanceFromTop, &b[i].bottomRight);
move(0, -distanceFromTop, &b[i].topRight);
}
else
{
move(0, distanceFromBottom, &b[i].bottomLeft);
move(0, distanceFromBottom, &b[i].topLeft);
move(0, distanceFromBottom, &b[i].bottomRight);
move(0, distanceFromBottom, &b[i].topRight);
}
}
// check if Inside Row2 and Row3
else if ((b[i].bottomLeft.y < ROW2.topLeft.y) && (b[i].topLeft.y > ROW2.topLeft.y))
{
//System.out.println("ROW2 and ROW3 Executed");
distanceFromBottom = ROW2.topLeft.y - b[i].bottomLeft.y;
distanceFromTop = b[i].height - distanceFromBottom;
if (distanceFromBottom > distanceFromTop)
{
move(0, -distanceFromTop, &b[i].bottomLeft);
move(0, -distanceFromTop, &b[i].topLeft);
move(0, -distanceFromTop, &b[i].bottomRight);
move(0, -distanceFromTop, &b[i].topRight);
}
else
{
move(0, distanceFromBottom, &b[i].bottomLeft);
move(0, distanceFromBottom, &b[i].topLeft);
move(0, distanceFromBottom, &b[i].bottomRight);
move(0, distanceFromBottom, &b[i].topRight);
}
}
else if ((b[i].bottomLeft.y >= ROW1.bottomLeft.y) && (b[i].topLeft.y <= ROW1.topLeft.y))
{
// do nothing in ROW1
}
else if ((b[i].bottomLeft.y >= ROW2.bottomLeft.y) && (b[i].topLeft.y <= ROW2.topLeft.y))
{
// do nothing in ROW2
}
else // ((b[i].bottomLeft.y >=
// ROW2.bottomLeft.y)&&(b[i].topLeft.y <=
// ROW2.topLeft.y))
{
// do nothing in ROW3
}
}
}
//use when floating point operations create run time problems
void linkfloat()
{
float a = 0.0, *x;
x = &a;
a = *x;
}
int findOverlaps(struct Block *b, int n)
{
int k, j, p = 0;
//struct Overlap OL[200];
//short flag = FALSE;
double overlapLength = 0.0;
for (int i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if (k == j)
continue;
overlapLength = 0.0;
//for ROW1
if (b[k].bottomLeft.y >= 0 && b[k].topLeft.y <= 100 && b[j].bottomLeft.y >= 0 && b[j].topLeft.y <= 100)
{
//if ((b[k].bottomRight.x > b[j].bottomLeft.x) && (b[k].bottomRight.y < b[j].topLeft.y || b[k].topRight.y > b[j].bottomLeft.y))
if ((b[k].bottomLeft.y == b[j].bottomLeft.y) && (b[k].bottomRight.x > b[j].bottomLeft.x))
{
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
OL[p].overlapLength = overlapLength;
OL[p].b1 = &b[k];
OL[p].b2 = &b[j];
p++;
}
else if (((b[k].topLeft.y > b[j].bottomLeft.y) && (b[k].topRight.x > b[j].bottomLeft.x)) || ((b[k].bottomLeft.y < b[j].topLeft.y) && (b[k].bottomRight.x > b[j].topLeft.x)))
{
//flag = TRUE;
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
OL[p].overlapLength = overlapLength;
OL[p].b1 = &b[k];
OL[p].b2 = &b[j];
p++;
}
else
{
}
overlapLength = 0.0;
}
//For Row2
else if (b[k].bottomLeft.y >= 100 && b[k].topLeft.y <= 200 && b[j].bottomLeft.y >= 100 && b[j].topLeft.y <= 200)
{
if ((b[k].bottomLeft.y == b[j].bottomLeft.y) && (b[k].bottomRight.x > b[j].bottomLeft.x))
{
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
OL[p].overlapLength = overlapLength;
OL[p].b1 = &b[k];
OL[p].b2 = &b[j];
p++;
}
else if (((b[k].topLeft.y > b[j].bottomLeft.y) && (b[k].topRight.x > b[j].bottomLeft.x)) || ((b[k].bottomLeft.y < b[j].topLeft.y) && (b[k].bottomRight.x > b[j].topLeft.x)))
{
//flag = TRUE;
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
OL[p].overlapLength = overlapLength;
OL[p].b1 = &b[k];
OL[p].b2 = &b[j];
p++;
}
else
{
}
overlapLength = 0.0;
}
//For Row3
else if (b[k].bottomLeft.y >= 200 && b[k].topLeft.y <= 300 && b[j].bottomLeft.y >= 200 && b[j].topLeft.y <= 300)
{
if ((b[k].bottomLeft.y == b[j].bottomLeft.y) && (b[k].bottomRight.x > b[j].bottomLeft.x))
{
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
OL[p].overlapLength = overlapLength;
OL[p].b1 = &b[k];
OL[p].b2 = &b[j];
p++;
}
else if (((b[k].topLeft.y > b[j].bottomLeft.y) && (b[k].topRight.x > b[j].bottomLeft.x)) || ((b[k].bottomLeft.y < b[j].topLeft.y) && (b[k].bottomRight.x > b[j].topLeft.x)))
{
//flag = TRUE;
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
OL[p].overlapLength = overlapLength;
OL[p].b1 = &b[k];
OL[p].b2 = &b[j];
p++;
}
else
{
}
overlapLength = 0.0;
}
else
{
//do nothing
}
}
}
return p;
}
struct Block *
getBlock(struct Block *b, int n, int id)
{
int i;
for (i = 0; i < n; i++)
{
if (id == b[i].id)
return (b + i);
}
return NULL;
}
void placeHorizontalGreedy(struct Block *b, int n)
{
struct Block *t = NULL;
int p = 0, k = 0;
p = findOverlaps(b, n);
qsort(OL, p, sizeof(struct Overlap), comparator);
while (TRUE)
{
t = getBlock(b, 12, OL[k].b2->id);
if (t != NULL)
{
//t->bottomLeft.x = t->bottomLeft.x+OL[0].overlapLength;
if ((t->bottomLeft.x + OL[0].overlapLength + t->width) <= ROW1.bottomRight.x)
{
move(OL[0].overlapLength, 0, &t->bottomLeft);
intialiseBlock(t);
}
}
p = findOverlaps(b, n);
if (p == 0)
break;
else
qsort(OL, p, sizeof(struct Overlap), comparator);
}
}
void placeHorizontalNonGreedy(struct Block *b, int n, struct Block ROW1,
struct Block ROW2, struct Block ROW3)
{
int k, j, p = 0;
double overlapLength = 0.0;
for (int i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if (k == j)
continue;
overlapLength = 0.0;
//check if in Row1
if (b[k].bottomLeft.y >= ROW1.bottomLeft.y && b[k].topLeft.y <= ROW1.topLeft.y && b[j].bottomLeft.y >= ROW1.bottomLeft.y && b[j].topLeft.y <= ROW1.topLeft.y)
{
if ((b[k].bottomRight.x > b[j].bottomLeft.x) && (b[k].bottomRight.y < b[j].topLeft.y || b[k].topRight.y > b[j].bottomLeft.y))
{
//flag = TRUE;
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
if (overlapLength != 0)
{
if (b[j].bottomLeft.x + overlapLength + b[j].width <=
ROW1.bottomRight.x)
{
b[j].bottomLeft.x += overlapLength;
intialiseBlock(&b[j]);
}
else
{
b[j].bottomLeft.x =
b[j].bottomLeft.x + (ROW1.bottomRight.x -
b[j].bottomRight.x);
}
}
}
}
//check if in Row2
else if (b[k].bottomLeft.y >= ROW2.bottomLeft.y && b[k].topLeft.y <= ROW2.topLeft.y && b[j].bottomLeft.y >= ROW2.bottomLeft.y && b[j].topLeft.y <= ROW2.topLeft.y)
{
if ((b[k].bottomRight.x > b[j].bottomLeft.x) && (b[k].bottomRight.y < b[j].topLeft.y || b[k].topRight.y > b[j].bottomLeft.y))
{
//flag = TRUE;
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
if (overlapLength != 0)
{
if (b[j].bottomLeft.x + overlapLength + b[j].width <=
400)
{
b[j].bottomLeft.x += overlapLength;
intialiseBlock(&b[j]);
}
}
}
}
//check if in Row3
else if (b[k].bottomLeft.y >= ROW3.bottomLeft.y && b[k].topLeft.y <= ROW3.topLeft.y && b[j].bottomLeft.y >= ROW3.bottomLeft.y && b[j].topLeft.y <= ROW3.topLeft.y)
{
if ((b[k].bottomRight.x > b[j].bottomLeft.x) && (b[k].bottomRight.y < b[j].topLeft.y || b[k].topRight.y > b[j].bottomLeft.y))
{
//flag = TRUE;
overlapLength = b[k].bottomRight.x - b[j].bottomLeft.x;
if (overlapLength != 0)
{
if (b[j].bottomLeft.x + overlapLength + b[j].width <=
400)
{
b[j].bottomLeft.x += overlapLength;
intialiseBlock(&b[j]);
}
}
}
}
else
{
//printf("Invalid input\n");
continue;
}
}
}
}
int main()
{
//struct Block b[16];
//struct Block b[4];
//struct Block *b;
//struct Block ROW1, ROW2, ROW3;
//FILE *fp = fopen("/home/jeetu/experimentprograms/recordcoord.txt", "w+");
//FILE *fp = fopen("/home/jeetu/experimentprograms/blockcoordinates.txt", "r");
int ncell = 0, width = 0, i = 0, totalncells = 0, id = 0;
double distanceFromBottom = 0.0;
double distanceFromTop = 0.0;
int x = 0, y = 0, temp = 0, d = 0;
printf("Enter number of cells: ");
scanf("%d", &ncell);
width = computeCellWidth(ncell);
//width = 40;
b = (struct Block *)malloc(3 * ncell * sizeof(struct Block));
//b = (struct Block *)malloc(12 * sizeof(struct Block));
srand(time(0));
ROW1.bottomLeft.x = 0;
ROW1.bottomLeft.y = 0;
ROW1.height = 100;
ROW1.width = 400;
ROW1.whitespace = 40000;
intialiseBlock(&ROW1);
ROW2.bottomLeft.x = 0;
ROW2.bottomLeft.y = 100;
ROW2.height = 100;
ROW2.width = 400;
ROW2.whitespace = 40000;
intialiseBlock(&ROW2);
ROW3.bottomLeft.x = 0;
ROW3.bottomLeft.y = 200;
ROW3.height = 100;
ROW3.width = 400;
ROW3.whitespace = 40000;
intialiseBlock(&ROW3);
for (i = 0; i < ncell; i++)
{
if (i == 0)
{
// x = rand_interval(0, width / 2);
x = rand() % (width / 2);
//y = rand_interval(0, 70);
y = rand() % 70;
temp = x;
}
//m = rand();
else
{
x = rand_interval((temp + (int)width / 2), temp + width);
//x = (rand()%((temp+width)-(temp+width/2)))+(temp+width/2);
//y = rand_interval(0, 70);
y = rand() % 70;
temp = x;
}
b[totalncells].bottomLeft.x = x;
b[totalncells].bottomLeft.y = y;
b[totalncells].width = width;
b[totalncells].height = 50;
b[totalncells].id = id;
//initial white space calculation for ROW1
if (b[totalncells].bottomLeft.y >= ROW1.bottomLeft.y && b[totalncells].topLeft.y <= ROW1.topLeft.y)
{
ROW1.whitespace =
ROW1.whitespace - b[totalncells].width * b[totalncells].height;
}
id++;
intialiseBlock(&b[totalncells]);
totalncells++;
//printf("x=%d\ty=%d\n", x, y);
}
temp = 0;
//Generate random coordinates For Row2
for (i = 0; i < ncell; i++)
{
//int x = 0, y = 0, m = 0, d = 0;
if (i == 0)
{
/*x = rand_interval(0, width / 2);
y = rand_interval(100, 170); */
//x = rand() % (width / 2);
x = rand() % (width / 2);
y = (rand() % (170 - 100)) + 100;
temp = x;
}
//m = rand();
else
{
/*x = rand_interval((temp + (int)width / 2), 400 - (int)width);
y = rand_interval(100, 170); */
//x = (rand()%((temp+width)-(temp+width/2)))+(temp+width/2);
x = rand_interval((temp + (int)width / 2), temp + width);
y = (rand() % (170 - 100)) + 100;
temp = x;
}
b[totalncells].bottomLeft.x = x;
b[totalncells].bottomLeft.y = y;
b[totalncells].width = width;
b[totalncells].height = 50;
b[totalncells].id = id;
//initial white space calculation for ROW2
if (b[totalncells].bottomLeft.y >= ROW2.bottomLeft.y && b[totalncells].topLeft.y <= ROW2.topLeft.y)
{
ROW2.whitespace =
ROW2.whitespace - b[totalncells].width * b[totalncells].height;
}
id++;
intialiseBlock(&b[totalncells]);
totalncells++;
//printf("x=%d\ty=%d\n", x, y);
}
temp = 0;
//Generate random coordinates For Row3
for (i = 0; i < ncell; i++)
{
//int x = rand_interval(0, 100 - (int)width);
//int y = rand_interval(20, 25); //cell height is 5 so max will reach 30
//int x = 0, y = 0, m = 0, d = 0;
if (i == 0)
{
/*x = rand_interval(0, width / 2);
y = rand_interval(200, 250); */
x = rand() % (width / 2);
y = (rand() % (250 - 200)) + 200;
temp = x;
}
//m = rand();
else
{
/*x = rand_interval((temp + (int)width / 2), 400 - (int)width);
y = rand_interval(200, 250); */
x = rand_interval((temp + (int)width / 2), temp + width);
//x = (rand()%((temp+width)-(temp+width/2)))+(temp+width/2);
y = (rand() % (250 - 200)) + 200;
temp = x;
}
b[totalncells].bottomLeft.x = x;
b[totalncells].bottomLeft.y = y;
b[totalncells].width = width;
b[totalncells].height = 50;
b[totalncells].id = id;
//initial white space calculation for ROW3
if (b[totalncells].bottomLeft.y >= ROW3.bottomLeft.y && b[totalncells].topLeft.y <= ROW3.topLeft.y)
{
ROW3.whitespace =
ROW3.whitespace - b[totalncells].width * b[totalncells].height;
}
id++;
intialiseBlock(&b[totalncells]);
totalncells++;
//printf("x=%d\ty=%d\n", x, y);
}
for (i = 0; i < totalncells; i++)
{
//printf("%f %f %f %f\n", b[i].bottomLeft.x, b[i].bottomLeft.y, b[i].width, b[i].height);
printf("%d\t%d\t%d\t%d\n", (int)b[i].bottomLeft.x,
(int)b[i].bottomLeft.y, (int)b[i].width, (int)b[i].height);
}
placeVertical(b, totalncells, ROW1, ROW2, ROW3);
//placeVerticalWithDensityConstraints(b,totalncells,ROW1,ROW2,ROW3);
printf("\nAfter vertical Updation\n");
for (i = 0; i < totalncells; i++)
{
printf("%d\t%d\t%d\t%d\n", (int)b[i].bottomLeft.x,
(int)b[i].bottomLeft.y, (int)b[i].width, (int)b[i].height);
}
/*placeHorizontalNonGreedy(b, 12, ROW1, ROW2, ROW3);
printf("\nAfter Horizontal Updation(Non greedy)\n");
for (i = 0; i < 12; i++)
{
printf("%d\t%d\t%d\t%d\n", (int)b[i].bottomLeft.x, (int)b[i].bottomLeft.y, (int)b[i].width, (int)b[i].height);
} */
placeHorizontalGreedy(b, totalncells);
printf("\nAfter Horizontal Updation(Greedy)\n");
for (i = 0; i < totalncells; i++)
{
printf("%d\t%d\t%d\t%d\n", (int)b[i].bottomLeft.x,
(int)b[i].bottomLeft.y, (int)b[i].width, (int)b[i].height);
}
return 0;
}
Explanation about the code
in this program I have defined three structures
one to represent a Point,one to represent rectangular block with 4 coordinates,one to represent overlap between pair of cells.
There is function(computeCellWidth) to dynamically compute width of cells according to a formula.
There are three rows ROW1,ROW2,ROW3 in which cells will reside.
This program will first ask for number of cells and this is numbers of cells per row.At first x,y coordinates for each cell is randomly generated.Then placeVertical() function will move cells to different rows which are spanning across two rows.Then there is a placeHorizontalGreedy() function which will remove overlap among cells by first finding overlaplength and then sorting them and removing the overlap between cells which has least overlap and this process will continue till either there will be no overlaps or overlap removal is not possible anymore.Here I would like to bring to the notice of everybody that cells will be moved towards the right of the grid to remove overlaps.In placeHorizontalGreedy() function I have used a variable p which will indicate number of overlaps after each iteration.for small number of cells testing for p to be zero works fine but not as I increase the numbers.Plus how can I make sure that overlaps exists but overlap removal is not possible anymore,so that I can stop the loop. Please help me out.

CS50x - Filter More

I doing filter of images bmp in c. The pset required the Sobel Operator. I don't know where I being mistake.
Help me please.
I'm basically making a copy of my image (because the original will be changed.)
Then I take the 3x3 values to put in the formula
So we add and multiply
Finally I take the result and put it in the formula: square root (Gx ^ 2 + Gy ^ 2)
If it exceeds 255 it must be 255, because RGB goes up to 255 which is white
And if there is a broken number, round to the nearest
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Variáveis
RGBTRIPLE temp[height][width];
int GR[3][3];
int GG[3][3];
int GB[3][3];
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
float resultR, resultG, resultB;
// Cópia temporária do original
for (int tempi = 0; tempi < height; tempi++)
{
for (int tempj = 0; tempj < width; tempj++)
{
temp[tempi][tempj].rgbtRed = image[tempi][tempj].rgbtRed;
temp[tempi][tempj].rgbtGreen = image[tempi][tempj].rgbtGreen;
temp[tempi][tempj].rgbtBlue = image[tempi][tempj].rgbtBlue;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int countx = 0;
// Pegar 3X3
for (int x = i - 1; x < i + 2; x++)
{
int county = 0;
for (int y = j - 1; y < j + 2; y++)
{
if ((x < 0 || y < 0) || (x >= height || y >= width))
{
GR[countx][county] = 0;
GG[countx][county] = 0;
GB[countx][county] = 0;
}
else
{
GR[countx][county] = temp[x][y].rgbtRed;
GG[countx][county] = temp[x][y].rgbtGreen;
GB[countx][county] = temp[x][y].rgbtBlue;
}
county++;
}
countx++;
}
float sumxR = 0, sumyR = 0, sumxG = 0, sumyG = 0, sumxB = 0, sumyB = 0;
for (int ix = 0; ix <= 2; ix++)
{
for (int iy = 0; iy <= 2; iy++)
{
sumxR = sumxR + (GR[ix][iy] * Gx[ix][iy]);
sumxG = sumxG + (GG[ix][iy] * Gx[ix][iy]);
sumxB = sumxB + (GB[ix][iy] * Gx[ix][iy]);
sumyR = sumyR + (GR[ix][iy] * Gy[ix][iy]);
sumyG = sumyG + (GG[ix][iy] * Gy[ix][iy]);
sumyB = sumyB + (GB[ix][iy] * Gy[ix][iy]);
}
}
resultR = sqrt(sumxR * sumxR) + sqrt(sumyR * sumyR);
resultG = sqrt(sumxG * sumxG) + sqrt(sumyG * sumyG);
resultB = sqrt(sumxB * sumxB) + sqrt(sumyB * sumyB);
if (resultR > 255)
{
resultR = 255;
}
if (resultG > 255)
{
resultG = 255;
}
if (resultB > 255)
{
resultB = 255;
}
image[i][j].rgbtRed = round(resultR);
image[i][j].rgbtGreen = round(resultG);
image[i][j].rgbtBlue = round(resultB);
}
}
}
You need to changesqrt(sumxR * sumxR) + sqrt(sumyR * sumyR) to sqrt((sumxR * sumxR) + (sumyR * sumyR)) and they are not the same

cs50 pset4 - edge detection filter not working - sobel operator

The following code i have written has the purpose of detecting the edges in a pixel using the sobel operator. However, it fails all of the tests given by check50 (tool offered by cs50). The output image is also the exact same as the input.
before you continue reading, visit the pset's link
note: I'm supposed to form a 3x3 grid around the pixel I want to filter so that I can iterate over each value in the GX and GY values. I've used ints hh and ww to do this.
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int sqrtRedd;
int sqrtGreenn;
int sqrtBluee;
//make copy of image
RGBTRIPLE copy[height][width];
for(int h = 0; h < height; h++)
{
for(int w = 0; w < width; w++)
{
copy[h][w] = image[h][w];
}
}
//loop through pixels
for(int h = 0; h < height; h++)
{
for(int w = 0; w < width; w++)
{
int GXred = 0;
int GYred = 0;
int GXgreen = 0;
int GYgreen = 0;
int GXblue = 0;
int GYblue = 0;
for(int hh = -1; hh <= 1; hh++)
{
for(int ww = -1; ww <= 1; ww++)
{
if( h + hh >= 0 && h + hh < height && w + ww >= 0 && w + ww < width)
{
//form 3x3 grid
GXred += ww * copy[2 - hh * hh][2 - ww * ww].rgbtRed;
GYred += hh * copy[2 - hh * hh][2 - ww * ww].rgbtRed;
GXgreen += ww * copy[2 - hh * hh][2 - ww * ww].rgbtGreen;
GYgreen += hh * copy[2 - hh * hh][2 - ww * ww].rgbtGreen;
GXblue += ww * copy[2 - hh * hh][2 - ww * ww].rgbtBlue;
GXblue += hh * copy[2 - hh * hh][2 - ww * ww].rgbtBlue;
}
}
}
int red = round(sqrt(GXred * GXred + GYred * GYred));
int green = round(sqrt(GXgreen * GXgreen + GYgreen * GXgreen));
int blue = round(sqrt(GXblue * GXblue + GYblue * GYblue));
if(red > 225)
{
red = 225;
}
else if(green > 225)
{
green = 225;
}
else if(blue > 225)
{
blue = 225
}
image[h][w].rgbtRed = red;
image[h][w].rgbtGreen = green;
image[h][w].rgbtBlue = blue;
}
}
return;
}
RGBTRIPLE:
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
These are the error messages given by check50:
:( edges correctly filters middle pixel
expected "210 150 60\n", not "143 105 30\n"
:( edges correctly filters pixel on edge
expected "213 228 255\n", not "164 144 79\n"
:( edges correctly filters pixel in corner
expected "76 117 255\n", not "58 77 64\n"
:( edges correctly filters 3x3 image
expected "76 117 255\n21...", not "58 77 64\n164 ..."
:( edges correctly filters 4x4 image
expected "76 117 255\n21...", not "58 77 64\n164 ..."
As you can see, the output values are way off and not even near what they should actually be. the problem is: I don't know if these errors are caused by a) my way of trying to find the GX and GY values from the kernels or b)by the way i'm applying the sobel operator.
I've tried finding GX and GY values in other ways (didn't work) such as:
if(hh == -1)
{
GYred += copy[h - 1][w + ww].rgbtRed * -1;
GYgreen += copy[h - 1][w + ww].rgbtGreen * -1;
GYblue += copy[h - 1][w + ww].rgbtBlue * -1;
}
else if( hh == 0)
{
GYred += copy[h][w + ww].rgbtRed * 0;
GYgreen += copy[h][w + ww].rgbtGreen * 0;
GYblue += copy[h][w + ww].rgbtBlue * 0;
}
else if(hh == 1)
{
GYred += copy[h + 1][w + ww].rgbtRed * 1;
GYgreen += copy[h + 1][w + ww].rgbtGreen * 1;
GYblue += copy[h + 1][w + ww].rgbtBlue * 2;
}
else if(hh == 2)
{
GYred += copy[h + 2][w + ww].rgbtRed * 2;
GYgreen += copy[h + 2][w + ww].rgbtGreen * 2;
GYblue += copy[h + 2][w + ww].rgbtBlue * 2;
}
//start setting GX values
if(ww == -2)
{
GXred += copy[h + hh][w - 2].rgbtRed * -2;
GXgreen += copy[h + hh][w - 2].rgbtGreen * -2;
GXblue += copy[h + hh][w - 2].rgbtBlue * -2;
}
else if(ww == -1)
{
GXred += copy[h + hh][w - 1].rgbtRed * -1;
GXgreen += copy[h + hh][w - 1].rgbtGreen * -1;
GXblue += copy[h + hh][w - 1].rgbtBlue * -1;
}
else if(ww == 0)
{
GXred += copy[h + hh][w].rgbtRed * 0;
GXgreen += copy[h + hh][w].rgbtGreen * 0;
GXblue += copy[h + hh][w].rgbtBlue * 0;
}
else if(ww == 1)
{
GXred += copy[h + hh][w + 1].rgbtRed * 1;
GXgreen += copy[h + hh][w + 1].rgbtGreen * 1;
GXblue += copy[h + hh][w + 1].rgbtBlue * 1;
}
I've been stuck on this pset for almost a week now and so at this point I don't know what else to try.
You have quite some parts that do not really make sense or are missing.
You do not apply the sobel factors anywhere. Just taking the offset inside your 3x3 grid does not yield the correct values,
You only limit 1 color channel in case of overflow,
You limit to 225 instead of 255,
You mixed GXblue and GYblue, same for GXgreen and GYgreen.
I have prepared a new version that should do the trick.
Now tested and with test data from initially failed test for 4x4 image.
#include <stdio.h>
#include <math.h>
typedef unsigned char BYTE;
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int sqrtRedd;
int sqrtGreenn;
int sqrtBluee;
//make copy of image
RGBTRIPLE copy[height][width];
for(int h = 0; h < height; h++)
{
for(int w = 0; w < width; w++)
{
copy[h][w] = image[h][w];
}
}
//loop through pixels
for(int h = 0; h < height; h++)
{
for(int w = 0; w < width; w++)
{
int GXred = 0;
int GYred = 0;
int GXgreen = 0;
int GYgreen = 0;
int GXblue = 0;
int GYblue = 0;
int index = 0;
int factorsX[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
int factorsY[] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
//form 3x3 grid
for(int hh = -1; hh <= 1; hh++)
{
for(int ww = -1; ww <= 1; ww++)
{
int x = w+ww;
int y = h+hh;
if( y >= 0 && y < height && x >= 0 && x < width)
{
GXred += factorsX[index] * copy[y][x].rgbtRed;
GYred += factorsY[index] * copy[y][x].rgbtRed;
GXgreen += factorsX[index] * copy[y][x].rgbtGreen;
GYgreen += factorsY[index] * copy[y][x].rgbtGreen;
GXblue += factorsX[index] * copy[y][x].rgbtBlue;
GYblue += factorsY[index] * copy[y][x].rgbtBlue;
}
index++;
}
}
int red = round(sqrt(GXred * GXred + GYred * GYred));
int green = round(sqrt(GXgreen * GXgreen + GYgreen * GYgreen));
int blue = round(sqrt(GXblue * GXblue + GYblue * GYblue));
if(red > 255)
{
red = 255;
}
if(green > 255)
{
green = 255;
}
if(blue > 255)
{
blue = 255;
}
image[h][w].rgbtRed = red;
image[h][w].rgbtGreen = green;
image[h][w].rgbtBlue = blue;
}
}
return;
}
int main(void)
{
RGBTRIPLE test_4x4[4][4] = {
{{0, 10, 25}, {0, 10, 30}, {40, 60, 80}, {50, 60, 80}},
{{20, 30, 90}, {30, 40, 100}, {80, 70, 90}, {80, 80, 90}},
{{20, 20, 40}, {30, 10, 30}, {50, 40, 10}, {50, 40, 100}},
{{50, 20, 40}, {50, 20, 40}, {50, 40, 80}, {50, 40, 80}},
};
edges(4, 4, test_4x4);
for(int h = 0; h < 4; h++)
{
for(int w = 0; w < 4; w++)
{
printf("%d %d %d\n", test_4x4[h][w].rgbtBlue, test_4x4[h][w].rgbtGreen, test_4x4[h][w].rgbtRed);
}
}
return 0;
}

CS50 filter: I'm getting a new error which I'm not familiar with

I've been working on the edge filter function for cs50 for a few hours now. It looks fine to me but I'm receiving a pretty scary looking error which I don't understand here is my code:
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
copy[h][w] = image[h][w];
}
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float gxred = 0;
float gxgreen = 0;
float gxblue = 0;
float gyred = 0;
float gygreen = 0;
float gyblue = 0;
for (int r = -1; r < 2; r++)
{
for (int s = -1; s < 2; s++)
{
if ((y + r < height && y + r >= 0) && (x + s < width && x + s >= 0))
{
if (r == -1)
{
gxred = copy[y + r][x + s].rgbtRed * s;
gxgreen = copy[y + r][x + s].rgbtGreen * s;
gxblue = copy[y + r][x + s].rgbtBlue * s;
gyred = copy[x + s][y + r].rgbtRed * s;
gygreen = copy[x + s][y + r].rgbtGreen * s;
gyblue = copy[x + s][y + r].rgbtBlue * s;
}
if (r == 0)
{
gxred = copy[y + r][x + s].rgbtRed * (s * 2);
gxgreen = copy[y + r][x + s].rgbtGreen * (s * 2);
gxblue = copy[y + r][x + s].rgbtBlue * (s * 2);
gyred = copy[x + s][y + r].rgbtRed * (s * 2);
gygreen = copy[x + s][y + r].rgbtGreen * (s * 2);
gyblue = copy[x + s][y + r].rgbtBlue * (s * 2);
}
if (r == 1)
{
if (s * s == 1)
{
gxred = copy[y + r][x + s].rgbtRed * (s * s);
gxgreen = copy[y + r][x + s].rgbtGreen * (s * s);
gxblue = copy[y + r][x + s].rgbtBlue * (s * s);
gyred = copy[x + s][y + r].rgbtRed * (s * s);
gygreen = copy[x + s][y + r].rgbtGreen * (s * s);
gyblue = copy[x + s][y + r].rgbtBlue * (s * s);
}
else
{
gxred = copy[y + r][x + s].rgbtRed * (s + 1);
gxgreen = copy[y + r][x + s].rgbtGreen * (s + 1);
gxblue = copy[y + r][x + s].rgbtBlue * (s + 1);
gyred = copy[x + s][y + r].rgbtRed * (s + 1);
gygreen = copy[x + s][y + r].rgbtGreen * (s + 1);
gyblue = copy[x + s][y + r].rgbtBlue * (s + 1);
}
}
}
}
}
image[y][x].rgbtRed = round(sqrt((gxred * gxred) + (gyred * gyred)));
image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
image[y][x].rgbtBlue = round(sqrt((gxblue * gxblue) + (gyblue * gyblue)));
image[y][x].rgbtRed = image[y][x].rgbtRed % 256;
image[y][x].rgbtGreen = image[y][x].rgbtGreen % 256;
image[y][x].rgbtBlue = image[y][x].rgbtBlue % 256;
}
}
return;
}
And here is my error
helpers.c:169:37: runtime error: 261 is outside the range of representable values of type
'unsigned char'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==5405==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7ffea18b01fa (pc
0x000000429bb3 bp 0x7ffea18af1a0 sp 0x7ffea17fe250 T5405)
==5405==The signal is caused by a READ memory access.
#0 0x429bb2 (/home/ubuntu/pset4/filter/filter+0x429bb2)
#1 0x42330e (/home/ubuntu/pset4/filter/filter+0x42330e)
#2 0x7f7c0b22eb96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#3 0x402e19 (/home/ubuntu/pset4/filter/filter+0x402e19)
UndefinedBehaviorSanitizer can not provide additional info.
==5405==ABORTING
This directs me to this line at the end:
image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
I'd be really grateful if someone could explain what this kind of error means. And also, any critique on my code would be much appreciated, I'm trying to get good at this.
RGB values can be any value between 0 and 255.
You are somehow assigning your RGB value in that line to 261 which is outside of the range.
I would print the value in the following code before assigning to rgb value.
round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)))
Then check why inside of round gives 261. Once you fix this you are good to go.

Resources