Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Created January 4, 2022 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heatblazer/83e86f7f2036c48cfd77ab5d55fd56ff to your computer and use it in GitHub Desktop.
Save heatblazer/83e86f7f2036c48cfd77ab5d55fd56ff to your computer and use it in GitHub Desktop.
// SAT.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <vector>
int main()
{
static const int W = 5;
static const int H = 5;
int counter = 1;
std::vector<std::vector<int> > Img;
std::vector<std::vector<int> > SAT;
Img.resize(W);
SAT.resize(W);
for (int i = 0; i < W; i++) {
Img[i].resize(H);
SAT[i].resize(H);
}
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
Img[i][j] = counter++;
}
}
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
std::cout << "[" << Img[i][j] << "]";
}
std::cout << "\r\n";
}
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
int val = Img[i][j];
if (i > 0) {
val += SAT[i - 1][j];
}
if (j > 0) {
val += SAT[i][j - 1];
}
if (i > 0 && j > 0)
val -= SAT[i - 1][j - 1];
SAT[i][j] = val;
}
}
#if 1
std::cout << "SUM:\r\n";
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
std::cout << "[" << SAT[i][j] << "]";
}
std::cout << "\r\n" ;
}
#endif
/* get correct values from summed-area table,
* (take care about out of bounce errors) */
int x1, y1, x2, y2;
x1 = 0;
y1 = 0;
x2 = 2;
y2 = 2;
int A = SAT[x1+1][y1+1];
int D = SAT[x2][y2];
int B = SAT[x2][y2 - 1];
int C = SAT[x2 - 1][y2];
std::cout << A << "," << B << "," << C << "," << D << "\r\n";
std::cout << "SAT IS: " << D - C - B + A << "\r\n";
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment