点在哪里

题目描述:

输出一组图形(矩形或圆)和一组点的数据,判断点的位置。

 

输入格式:

输入一组图形的数据,其中每行以“c”开头表示圆,以“r”开头表示矩形,其中矩形是依次给出左下脚和右上角坐标,圆是给出圆心坐标及半径,图形数据以输入另起一行的“*”结束,接下来是给出点的坐标(xy),整个输入文件以9999.9  9999.9结束,每行中的各个数据以空格分隔。

 

输出格式:

输出点的位置如下信息:直接输出到屏幕上,如某点在图形上,则输出所有图形上的信息:Point i is conatined in figure j;如果点不在任一图形内,则输出:Point i is not contained in any figure

 

输入输出样例:

Sample input

Output for the input

r 0.0 0.0 5.5 10.3

c -5.0 -5.0 3.7

r 2.5 2.5 12.5 12.5

*

2.0 2.0

4.7 5.3

9999.9 9999.9

Point 1 is contained in figure 1

Point 2 is contained in figure 1

Point 2 is contained in figure 3

 

Sample input

Output for the input

r 8.5 -8.5 25.5 17.0

c 20.2 7.3 5.8

r 0.0 0.0 5.5 10.3

c -5.0 -5.0 3.7

r 2.5 2.5 12.5 12.5

c 5.0 15.0 7.2

*

6.9 11.2

20.0 20.0

17.6 3.2

-5.2 -7.8

4.7 5.3

2.0 2.0

9999.9 9999.9

Point 1 is contained in figure 5

Point 1 is contained in figure 6

Point 2 is not contained in any figure

Point 3 is contained in figure 1

Point 3 is contained in figure 2

Point 4 is contained in figure 4

Point 5 is contained in figure 3

Point 5 is contained in figure 5

Point 6 is contained in figure 3

 

解:

如果点(x,y)在矩形(x1,y1),(x2,y2)内,一定满足条件:x1<=x<=x2 && y1<=y<=y2

如果点(x,y)在圆(x1,y1,r)内,一定满足条件:sqrt((x-x1)2+(y-y1)2)<=r

                                                                                                                  .

 

代码:

 

 
#include<stdio.h>
#include
<math.h>
#include
<string.h>
#include
<stdlib.h>


#define MAXLEN 1000
#define RGET(TYPE,VAR,LOC) ((TRectangle *)TYPE[LOC].data)->VAR
#define CGET(TYPE,VAR,LOC) ((TCircle *)TYPE[LOC].data)->VAR

struct TRectangle{    //矩形
    float x1,x2,y1,y2;
}
;

struct TCircle{    //
    float x,y,r;
}
;

class Figure{
public:
    
void *data;
    
char type;    //type='r' 表示矩形;type='c' 表示圆
}
;

int figlen;
Figure figure[MAXLEN];


inline 
void
swap(
float &a,float &b){    //交换a和b的值
    float c;
    c
=a;
    a
=b;
    b
=c;
}


inline 
bool
equal(
const float a,const float b)//判断俩浮点数是否相等
    return fabs(a-b)<1e-6;
}


inline 
void 
readRecord()
{    //读取图形数据
    char type;
    
while(1){
        type
=getchar();
        
if(type=='*'return;
        figure[figlen].type
=type;
        
if(type=='r'){
            figure[figlen].data
=new TRectangle;
            scanf(
"%f%f%f%f",&RGET(figure,x1,figlen),&RGET(figure,y1,figlen),&RGET(figure,x2,figlen),&RGET(figure,y2,figlen));
            figlen
++;
        }

        
else if(type=='c'){
            figure[figlen].data
=new TCircle;
            scanf(
"%f%f%f",&CGET(figure,x,figlen),&CGET(figure,y,figlen),&CGET(figure,r,figlen));
            figlen
++;
        }

    }

}


inline 
void 
caculate()
{    //输入点并输出所在图形
    float a,b,c,d;
    
int i,no=0;
    
bool ok;
    
while(1){
        scanf(
"%f%f",&a,&b);
        
if(equal(a,9999.9f&& equal(b,9999.9f)) return;
        
++no;
        ok
=false;
        
for(i=0;i<figlen;i++)
            
if(figure[i].type=='r'){
                
if(RGET(figure,x1,i)<&& RGET(figure,x2,i)>&& RGET(figure,y1,i)<&& RGET(figure,y2,i)>b){
                    printf(
"Point %d is contained in figure %d ",no,i+1);
                    ok
=true;
                }

            }

            
else{
                c
=CGET(figure,x,i)-a;
                c
*=c;
                d
=CGET(figure,y,i)-b;
                d
*=d;
                
if(sqrt(c+d)<CGET(figure,r,i) || equal((float)sqrt(c+d),CGET(figure,r,i))){
                    printf(
"Point %d is contained in figure %d ",no,i+1);
                    ok
=true;
                }

            }

        
if(ok==false) printf("Point %d is not contained in any figure ",no);
    }

}


void
main()
{
    
int fd;
    fd
=open("test.txt",O_RDONLY);
    dup2(fd,
0);
    fd
=open("result.txt",O_TRUNC|O_WRONLY);
    dup2(fd,
1);
    figlen
=0;
    readRecord();
    caculate();
}

关键修改集中在以下几个方面,确保实现**透明背景**和**阻断击穿透**: --- ### **1. 移除滚动区域的白色背景** #### **修改**: - **`NSScrollView` 和 `NSTableView` 的背景透明化** ```swift scrollView.drawsBackground = false // 禁用 ScrollView 背景 scrollView.backgroundColor = .clear // 确保背景色透明 scrollView.contentView.drawsBackground = false // 禁用内容视图背景 tableView.backgroundColor = .clear // 禁用 TableView 背景 ``` - 这些设置会移除 `NSScrollView` 和 `NSTableView` 默认的白色背景,仅显示单元格内容。 - **单元格背景透明** 在 `tableView(_:viewFor:row:)` 中设置: ```swift cell.wantsLayer = true cell.layer?.backgroundColor = NSColor.clear.cgColor ``` - 确保每个 `CardTableCellView` 也是透明的。 --- ### **2. 阻断击穿透** #### **修改**: - **重写 `mouseDown(with:)` 拦截击事件** ```swift override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) // 拦截所有击 } ``` - 直接拦截击事件,防止事件穿透到下层视图。 - **添加透明覆盖层(可选增强方案)** ```swift private func setupBackground() { let overlay = NSView() overlay.wantsLayer = true overlay.layer?.backgroundColor = NSColor.clear.cgColor view.addSubview(overlay, positioned: .below, relativeTo: scrollView1) // 添加覆盖全屏的约束... } ``` - 通过添加一个透明覆盖层,确保表格间隙区域也无法击下层视图。 --- ### **3. 动态行高支持** #### **修改**: - **禁用固定行高** ```swift tableView.rowHeight = 0 // 必须设为 0 才能触发 heightOfRow ``` - 这是动态行高的关键配置,否则 `heightOfRow` 不会被调用。 - **实现 `heightOfRow` 方法** ```swift func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { let cell = tableView.makeView(withIdentifier: id, owner: self) as? CardTableCellView cell?.configure(with: data) // 配置数据 cell?.layoutSubtreeIfNeeded() // 强制布局 return cell?.cellHeight ?? 100 // 返回动态高度 } ``` - 通过临时创建单元格并强制布局,获取其内容高度。 --- ### **4. 布局约束优化** #### **修改**: - **确保 `NSScrollView` 正确约束** ```swift NSLayoutConstraint.activate([ scrollView1.topAnchor.constraint(equalTo: view.topAnchor, constant: 20), scrollView1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20), scrollView1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20), scrollView1.heightAnchor.constraint(equalToConstant: 300), // 第二个表格的约束... ]) ``` - 避免约束冲突导致布局异常。 --- ### **总结:核心修改代码片段** ```swift // 1. 透明化配置 scrollView.drawsBackground = false scrollView.contentView.drawsBackground = false tableView.backgroundColor = .clear // 2. 阻断击 override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) } // 3. 动态行高 tableView.rowHeight = 0 func tableView(_:heightOfRow:) -> CGFloat { /* 计算动态高度 */ } ``` 这些修改直接解决了背景透明、击穿透和动态行高的问题,同时保持代码简洁高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值