Manipulating a diagonal line in C++ - c++

I am playing with using nested for-loops to plot pixels and basically draw flags. So far I've figured out how to make circles, diagonal lines and crosses.
I am however not able to wrap my head around how to limit from where a straight line is to be drawn.
Basically I'm trying to figure out how I need to change the code I used to draw the diagonal lines in the union jack to make the swastika in the flag of nazi Germany. Any help would be much appreciated!
Here's my current code and a screenshot of what I get:
for (int x = 0; x < 240; x++)
{
for (int y = 0; y < 160; y++)
{
uint16_t cX = 120;
uint16_t cY = 80;
uint16_t r = 66;
// Makes line
if (x-100 < y * 240 / 240 + 20 && x-100 > y * 240 / 240 - 20)
{
PlotPixel16(x, y, black);
}
// Makes circle
else if (((x-cX)*(x-cX))+((y-cY)*(y-cY)) < r*r)
{
PlotPixel16(x, y, white);
}
else
{
PlotPixel16(x, y, red);
}
}
}
Screenshot!

You're actually drawing a polygon defined by four lines. The equation of a line is y=mx+b, and you want to be either above or below the line. While this isn't how I would do it, it's keeping with the spirit of your approach to test that y-mx+b<0 (or >0) for four different pairs of m and b. That will give you one line segment, and you can get the rest similarly.
Right now, you're selecting a region between only two lines. That's why you're getting that image.

Related

p5.js - get a rectangle to move left and right repeatedly (bounce)

I'm trying out some sample code for a bigger project, and I'm having trouble getting my rectangle to bounce between two lines.
function draw() {
print(frameCount)
background(255)
var x = 150 + frameCount;
rect(x,200,15,15);
line(150,0,150,400);
line(250,0,250,400);
if (x >= 250) {
background(255)
x = 350-frameCount;
rect(x,200,15,15);
line(250,0,250,400);
line(150,0,150,400);
} if (x <= 145) {
background(255)
x = 145 + (frameCount % 100);
rect(x,200,15,15);
line(250,0,250,400);
line(150,0,150,400);
}
}
I'm getting the feeling that after the first instance, it's disregarding the original if statement, which dictates a bounce to the left. I'm really not sure what's going wrong, and any help would be appreciated.
You probably just want to store the current position and speed in a set of variables, and then move the rectangle based on those. Here's an example:
var x = 0;
var speed = 1;
function draw(){
x += speed;
if(x < 0 || x > width){
speed *= -1;
}
background(64);
line(x, 0, x, height);
}
I've written a tutorial on this available here. That's for regular Processing, but the ideas are the same in P5.js.

SDL Infinite Tile Background

I'm trying to create a tiled background in SDL, one that scrolls and continues to scroll indefinitely.
So, I came up with some code and tested it out. It works well enough, but only can travel 1920 pixels along the x axis and 1080 along the Y.
Here's my code:
void Background::render(SDL_Renderer *renderer){
int Xoffset = 0;
int Yoffset = 0;
for(int y = 0; (y * 411) < 1080; y++){
for(int x = 0; (x * 405) < 1920; x++){
Xoffset = 0;
Yoffset = 0;
if(GameManager::GetInstance().getGlobalX() + (405 * x) + 405 < 0){
Xoffset = 1920;
}
if(GameManager::GetInstance().getGlobalY() + (411 * y) + 411 < 0){
Yoffset = 1080;
}
SDL_Rect backRect = {GameManager::GetInstance().getGlobalX() + (405 * x) + Xoffset, GameManager::GetInstance().getGlobalY() + (411 * y) + Yoffset, 405, 411};
SDL_RenderCopy(renderer, ResourceManager::GetInstance().getTexture("background"), 0, &backRect);
}
}
}
The getGlobalX() and getGlobalY() are where the object should be relative to the player.
You should be able to draw the 1920x1080 background more than once.
The algorithm would look something like this.
Draw a background starting at (-1920,0) (completely out of the screen)
Draw another copy of the background, this time starting at (0,0).
Every frame, draw both backgrounds one pixel to the right, so you'll have a scrolling illusion, the end of the background exiting the right will come out from the left.
Once your background at step 1 has come to (0,0), draw another background at (-1920,0) and keep scrolling.
So basically, you push two backgrounds to the right and keep putting one on the left every time you need to. This should be simple to code.

Determine which tile is clicked in a window

I am drawing a tilemap on a SFML renderwindow. I want to determine which tile is clicked by the user, but I just cant seem to find a solution. First of all, each tile has 32 width and height.
What i try at the moment : Get the position of the click. Loop trough the tilemap until a tile is found which position is between 100. So if I click on (100,100) the tile should begin at (96,96) but this does not seem to work.
Here is my code snippet from the function getTile(mousepos x,mousepos y)
Tile* TileMap::getTile(int x, int y)
{
Tile *t = NULL;
for(int i = 0; i < tilemap.size(); i++)
{
for(int j = 0; j < tilemap[i].size(); j++)
{
if(x > tilemap[i][j].sprite.getPosition().x
&& x < (tilemap[i][j].sprite.getPosition().x+32))
{
if(y > tilemap[i][j].sprite.getPosition().y
&& y < (tilemap[i][j].sprite.getPosition().y+32))
{
t = &tilemap[i][j];
break;
}
}
}
}
return t;
}
Based on your code, I am going to assume that you are basing your tilemap on a 2d array of Tiles: tilemap[x][y]. I am also going to assume that tilemap[0][0] is the top left tile.
There should be a much easier way to find out which tile is being clicked on instead of testing every single tile.
If you are at 100,100 and tiles are 32x32, then we can get the x and y of the tile within the tilemap by doing something as simple as:
x = 100 / 32 = 3
y = 100 / 32 = 3
Therefor the tile in your tilemap that corresponds to a mouse position of (100,100) is tilemap[3][3].

How to draw a filled circle?

I'm creating bitmap/bmp files according to the specifications with my C code and I would like to draw simple primitives on my bitmap. The following code shows how I draw a rectangle on my bitmap:
if(curline->type == 1) // draw a rectangle
{
int xstart = curline->x;
int ystart = curline->y;
int width = curline->width + xstart;
int height = curline->height + ystart;
int x = 0;
int y = 0;
for(y = ystart; y < height; y++)
{
for(x = xstart; x < width; x++)
{
arr[x][y].blue = curline->blue;
arr[x][y].green = curline->green;
arr[x][y].red = curline->red;
}
}
printf("rect drawn.\n");
}
...
save_bitmap();
Example output:
So basically I'm setting the red, green and blue values for all pixels within the given x and y field.
Now I'd like to fill a circle by knowing its midpoint and radius. But how do I know which pixels are inside this circle and which pixels ain't? Any help would be appreciated, thanks for reading.
A point lies within the bounds of a circle if the distance from the point to the center of the circle is less than the radius of the circle.
Consider a point (x1,y1) compared to a circle with center (x2,y2) and radius r:
int dx = x2 - x1; // horizontal offset
int dy = y2 - y1; // vertical offset
if ( (dx*dx + dy*dy) <= (r*r) )
{
// set pixel color
}
You can also try the midpoint algorithm, here on wikipedia.

How to determine Scale of Line Graph based on Pixels/Height?

I have a problem due to my terrible math abilities, that I cannot figure out how to scale a graph based on the maximum and minimum values so that the whole graph will fit onto the graph-area (400x420) without parts of it being off the screen (based on a given equation by user).
Let's say I have this code, and it automatically draws squares and then the line graph based on these values. What is the formula (what do I multiply) to scale it so that it fits into the small graphing area?
vector<int> m_x;
vector<int> m_y; // gets automatically filled by user equation or values
int HeightInPixels = 420;// Graphing area size!!
int WidthInPixels = 400;
int best_max_y = GetMaxOfVector(m_y);
int best_min_y = GetMinOfVector(m_y);
m_row = 0;
m_col = 0;
y_magnitude = (HeightInPixels/(best_max_y+best_min_y)); // probably won't work
x_magnitude = (WidthInPixels/(int)m_x.size());
m_col = m_row = best_max_y; // number of vertical/horizontal lines to draw
////x_magnitude = (WidthInPixels/(int)m_x.size())/2; Doesn't work well
////y_magnitude = (HeightInPixels/(int)m_y.size())/2; Doesn't work well
ready = true; // we have values, graph it
Invalidate(); // uses WM_PAINT
////////////////////////////////////////////
/// Construction of Graph layout on WM_PAINT, before painting line graph
///////////////////////////////////////////
CPen pSilver(PS_SOLID, 1, RGB(150, 150, 150) ); // silver
CPen pDarkSilver(PS_SOLID, 2, RGB(120, 120, 120) ); // dark silver
dc.SelectObject( pSilver ); // silver color
CPoint pt( 620, 620 ); // origin
int left_side = 310;
int top_side = 30;
int bottom_side = 450;
int right_side = 710; // create a rectangle border
dc.Rectangle(left_side,top_side,right_side,bottom_side);
int origin = 310;
int xshift = 30;
int yshift = 30;
// draw scaled rows and columns
for(int r = 1; r <= colrow; r++){ // draw rows
pt.x = left_side;
pt.y = (ymagnitude)*r+top_side;
dc.MoveTo( pt );
pt.x = right_side;
dc.LineTo( pt );
for(int c = 1; c <= colrow; c++){
pt.x = left_side+c*(magnitude);
pt.y = top_side;
dc.MoveTo(pt);
pt.y = bottom_side;
dc.LineTo(pt);
} // draw columns
}
// grab the center of the graph on x and y dimension
int top_center = ((right_side-left_side)/2)+left_side;
int bottom_center = ((bottom_side-top_side)/2)+top_side;
You are using ax^2 + bx + c (quadratic equation). You will get list of (X,Y) values inserted by user.
Let us say 5 points you get are
(1,1)
(2,4)
(4,1)
(5,6)
(6,7)
So, here your best_max_y will be 7 and best_min_y will be 1.
Now you have total graph area is
Dx = right_side - left_side //here, 400 (710 - 310)
Dy = bottom_side - top_side //here, 420 (450 - 30)
So, you can calculate x_magnitude and y_magnitude using following equation :
x_magnitude = WidthInPixels / Dx;
y_magnitude = HeightInPixels / Dy;
What I did was to determine how many points I had going in the x and y directions, and then divide that by the x and y dimensions, then divide that by 3, as I wanted each minimum point to be three pixels, so it could be seen.
The trick then is that you have to aggregate the data so that you are showing several points with one point, so it may be the average of them, but that depends on what you are displaying.
Without knowing more about what you are doing it is hard to make a suggestion.
For this part, subtract, don't add:
best_max_y+best_min_y as you want the difference.
The only other thing would be to divide y_magnitude and x_magnitude by 3. That was an arbitrary number I came up with, just so the users could see the points, you may find some other number to work better.