You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现宽高输入的最大拟合像素化椭圆矩阵生成函数?

How to Create a Function for the Largest Fitting Ellipse Matrix (Any Language)

Absolutely, this is totally doable! The core logic relies on the standard ellipse equation to check which pixels lie inside the maximum ellipse that fits perfectly within your input width and height. Let’s break this down step by step, then share examples in multiple languages to show how portable this logic is.

Core Concept

The largest ellipse fitting a width × height matrix will have:

  • Center at (width/2, height/2) (accounting for integer/float differences)
  • Semi-major axis = width/2
  • Semi-minor axis = height/2

For any pixel at coordinates (x, y) (where x is the column index, y is the row index), we check if it satisfies the ellipse inequality:

((x - center_x)/semi_x)² + ((y - center_y)/semi_y)² ≤ 1

If yes, the pixel is part of the ellipse (we can mark it as 1, white, etc.; else 0/black).

Step-by-Step Implementation

  • Calculate the center coordinates (center_x = width / 2.0, center_y = height / 2.0)
  • Calculate semi-axes (semi_x = width / 2.0, semi_y = height / 2.0)
  • Iterate over every row (y from 0 to height-1) and column (x from 0 to width-1)
  • For each (x,y), compute the left-hand side of the inequality
  • Mark the pixel as part of the ellipse if the value is ≤ 1

Example Implementations

Python

def create_ellipse_matrix(width, height):
    center_x = width / 2.0
    center_y = height / 2.0
    semi_x = width / 2.0
    semi_y = height / 2.0
    
    ellipse_matrix = []
    for y in range(height):
        row = []
        for x in range(width):
            # Calculate the ellipse equation value
            val = ((x - center_x)/semi_x)**2 + ((y - center_y)/semi_y)**2
            # Mark 1 if inside/on ellipse, 0 otherwise
            row.append(1 if val <= 1 else 0)
        ellipse_matrix.append(row)
    return ellipse_matrix

# Test with your examples
print("14x27 Ellipse Matrix:")
matrix_14x27 = create_ellipse_matrix(14, 27)
for row in matrix_14x27:
    print(''.join(['█' if pixel else ' ' for pixel in row]))

print("\n38x21 Ellipse Matrix:")
matrix_38x21 = create_ellipse_matrix(38, 21)
for row in matrix_38x21:
    print(''.join(['█' if pixel else ' ' for pixel in row]))

JavaScript

function createEllipseMatrix(width, height) {
    const centerX = width / 2.0;
    const centerY = height / 2.0;
    const semiX = width / 2.0;
    const semiY = height / 2.0;
    
    const ellipseMatrix = [];
    for (let y = 0; y < height; y++) {
        const row = [];
        for (let x = 0; x < width; x++) {
            const val = Math.pow((x - centerX)/semiX, 2) + Math.pow((y - centerY)/semiY, 2);
            row.push(val <= 1 ? 1 : 0);
        }
        ellipseMatrix.push(row);
    }
    return ellipseMatrix;
}

// Test usage
const matrix14x27 = createEllipseMatrix(14, 27);
console.log("14x27 Ellipse Matrix:");
matrix14x27.forEach(row => console.log(row.map(p => p ? '█' : ' ').join('')));

C++

#include <vector>
#include <cmath>
#include <iostream>

using namespace std;

vector<vector<int>> createEllipseMatrix(int width, int height) {
    double centerX = width / 2.0;
    double centerY = height / 2.0;
    double semiX = width / 2.0;
    double semiY = height / 2.0;
    
    vector<vector<int>> ellipseMatrix(height, vector<int>(width));
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double val = pow((x - centerX)/semiX, 2) + pow((y - centerY)/semiY, 2);
            ellipseMatrix[y][x] = (val <= 1) ? 1 : 0;
        }
    }
    return ellipseMatrix;
}

// Test function
int main() {
    auto matrix14x27 = createEllipseMatrix(14, 27);
    cout << "14x27 Ellipse Matrix:\n";
    for (auto &row : matrix14x27) {
        for (int pixel : row) {
            cout << (pixel ? "█" : " ");
        }
        cout << "\n";
    }
    return 0;
}

Key Notes

  • Coordinate Systems: Some languages/libraries use row-major (y first, x second) indexing, while others might use column-major. Adjust the loops if needed to match your target system's convention.
  • Integer vs Float: Using floating-point calculations ensures we handle odd dimensions correctly (like your 27-height example, where the center is at 13.5).
  • Customization: You can swap 1/0 for other values (like RGB codes for images) depending on how you plan to use the matrix.

内容的提问来源于stack exchange,提问作者GSerum_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:42:49