Cohen-Sutherland算法实现求线段是否过区间

Cohen-Sutherland算法实现求线段是否过区间

public class SutherlandUtils {
    private static final int LEFT = 1;    // #0001 左
    private static final int RIGHT = 2;    // #0010 右
    private static final int BOTTOM = 4;    // #0100 下
    private static final int TOP = 8;    // #1000 上

    /**
     * 编码
     */
    private static int encode(float x, float y, float[] center, int radius) {
        int c = 0;
        if (x < center[0] - radius) {
            c = c | LEFT;
        } else if (x > center[0] + radius) {
            c = c | RIGHT;
        }

        if (y < center[1] - radius) {
            c = c | TOP;
        } else if (y > center[1] + radius) {
            c = c | BOTTOM;
        }
        return c;
    }

    /**
     * 求线段是否过区间
     *
     * @param startX 线段开始x位置
     * @param startY 线段开始y位置
     * @param endX   线段结束x位置
     * @param endY   线段结束y位置
     * @param center 区间中点
     * @param radius 区间范围
     */
    public static boolean isInBounds(float startX, float startY, float endX, float endY, float[] center, int radius) {
        int startCode = encode(startX, startY, center, radius);
        int endCode = encode(endX, endY, center, radius);
        while (startCode != 0 || endCode != 0) {//线段两端都不在区间内
            if ((startCode & endCode) != 0) {//线段完全不可能和区间相交
                return false;
            }

            int code = endCode;
            if (startCode != 0) {
                code = startCode;
            }

            float x = 0;
            float y = 0;

            if ((LEFT & code) != 0) {//点在线左边
                x = center[0] - radius;
                if (startX == endX) {
                    y = startY;
                } else {
                    y = getPoint(startY, endY, x, startX, endX);
                }
            } else if ((RIGHT & code) != 0) {//点在线右边
                x = center[0] + radius;
                if (endX == startX) {
                    y = startY;
                } else {
                    y = getPoint(startY, endY, x, startX, endX);
                }
            } else if ((BOTTOM & code) != 0) {//点在线下边
                y = center[1] + radius;
                if (endY == startY) {
                    x = startX;
                } else {
                    x = getPoint(startX, endX, y, startY, endY);
                }
            } else if ((TOP & code) != 0) {//点在线上边
                y = center[1] - radius;
                if (endY == startY) {
                    x = startX;
                } else {
                    x = getPoint(startX, endX, y, startY, endY);
                }
            }

            if (code == startCode) {
                startCode = encode(x, y, center, radius);
            } else {
                endCode = encode(x, y, center, radius);
            }
        }
        return true;
    }

    private static float getPoint(float startB, float endB, float z, float startA, float endA) {
        return startB + (endB - startB) * (z - startA) / (endA - startA);
    }
}
传送门

Cohen-Sutherland算法—python实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cohen-Sutherland算法是一种计算机图形学中用于线段裁剪的算法。下面是Python实现代码: ```python # 定义区域码 INSIDE = 0 # 0000 LEFT = 1 # 0001 RIGHT = 2 # 0010 BOTTOM = 4 # 0100 TOP = 8 # 1000 # 定义边界线的位置 x_min, y_min, x_max, y_max = 50, 50, 100, 100 # 计算区域码 def computeCode(x, y): code = INSIDE if x < x_min: code |= LEFT elif x > x_max: code |= RIGHT if y < y_min: code |= BOTTOM elif y > y_max: code |= TOP return code # Cohen-Sutherland算法 def cohenSutherlandClip(x1, y1, x2, y2): code1 = computeCode(x1, y1) code2 = computeCode(x2, y2) accept = False while True: # 线段完全在窗口内 if code1 == 0 and code2 == 0: accept = True break # 线段完全在窗口外 elif (code1 & code2) != 0: break else: # 线段部分在窗口内 x, y = 0, 0 code = code1 if code1 != 0 else code2 if code & TOP: x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1) y = y_max elif code & BOTTOM: x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1) y = y_min elif code & RIGHT: y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1) x = x_max elif code & LEFT: y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1) x = x_min if code == code1: x1, y1 = x, y code1 = computeCode(x1, y1) else: x2, y2 = x, y code2 = computeCode(x2, y2) if accept: print("Line accepted from %.2f,%.2f to %.2f,%.2f" % (x1, y1, x2, y2)) else: print("Line rejected") ``` 上述代码实现了一个裁剪窗口为矩形的Cohen-Sutherland算法。用户可以根据需要自行修改边界线的位置和大小。函数`cohenSutherlandClip()`接收四个参数,分别是线段的两个端点坐标`x1, y1, x2, y2`。如果线段在窗口内,则输出“Line accepted from (x1,y1) to (x2,y2)”;否则,输出“Line rejected”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值