Skip to content

Instantly share code, notes, and snippets.

@Shtille
Created October 13, 2025 23:16
Show Gist options
  • Select an option

  • Save Shtille/4465d7db13babdeeb0a6a92eefccd098 to your computer and use it in GitHub Desktop.

Select an option

Save Shtille/4465d7db13babdeeb0a6a92eefccd098 to your computer and use it in GitHub Desktop.
Transforms dash pattern to texture data
// Example program
#include <vector>
#include <cstdio>
struct Data {
int ref;
int type;
int start;
int end;
};
typedef std::vector<int> PatternArray;
typedef std::vector<Data> DataArray;
void encode_data(const PatternArray& pattern, DataArray& data)
{
const int pattern_size = static_cast<int>(pattern.size());
// Calc texture (data) size
int size = 0;
for (int length : pattern)
size += length;
data.resize(static_cast<size_t>(size));
const int cap_begin = 1;
const int distance_step = 1;
const int start_distance = -cap_begin;
int distance = start_distance;
int pattern_index = 0;
int dash_start = 0;
int dash_end = dash_start + pattern[pattern_index];
int ref = dash_start;
int type = (distance < ref) ? -1 : 0;
int breakpoint = (type == 1) ? dash_end : dash_start;
for (int i = 0; i < size; ++i)
{
if (type == -1 && distance >= breakpoint) // breakpoint = dash_start
{
// Change type from -1 to 0
type = 0;
// ref remains the same
breakpoint = dash_end;
}
if (type == 0 && distance >= breakpoint) // breakpoint = dash_end
{
// Change type from 0 to 1
type = 1;
ref = dash_end;
int space_length = pattern[(pattern_index+1) % pattern_size];
breakpoint = (dash_end + dash_end + space_length) / 2; // middle of space
}
if (type == 1 && distance >= breakpoint) // breakpoint = middle of space
{
// Next dash
type = -1;
int space_length = pattern[(pattern_index+1) % pattern_size];
dash_start = dash_end + space_length;
dash_end = dash_start + pattern[(pattern_index+2) % pattern_size];
pattern_index += 2;
ref = dash_start;
breakpoint = dash_start;
}
Data& d = data[i];
d.ref = ref;
d.type = type;
d.start = dash_start;
d.end = dash_end;
distance += distance_step;
}
}
void print_data(const DataArray& data)
{
printf("Data:\n");
for (DataArray::size_type i = 0; i < data.size(); ++i)
{
const Data& d = data[i];
printf("[%zu]: (ref = %i, type = %i, start = %i, end = %i)\n", i, d.ref, d.type, d.start, d.end);
}
}
int main()
{
PatternArray pattern = {2,4};
DataArray data;
encode_data(pattern, data);
print_data(data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment