You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package challenge ;
import basic.Shape ;
/**
* 圆形到椭圆形的转换器, 实现ShapeConverter接口
*/
public class CircleToEllipseConverter implements ShapeConverter {
private double stretchFactor ; // 拉伸因子,用于确定椭圆的长半轴
/**
* 构造方法
* @param stretchFactor 拉伸因子, 大于1时会将圆形拉伸成椭圆形
*/
public CircleToEllipseConverter ( double stretchFactor ) {
this . stretchFactor = Math . max ( stretchFactor , 1.0 ) ; // 确保拉伸因子至少为1
}
@Override
public Shape convert ( Shape sourceShape ) {
if ( ! canConvert ( sourceShape ) ) {
return null ;
}
Circle circle = ( Circle ) sourceShape ;
double radius = circle . getRadius ( ) ;
// 将圆形转换为椭圆形:保持颜色不变,半径作为短半轴,拉伸后的半径作为长半轴
double semiMinorAxis = radius ;
double semiMajorAxis = radius * stretchFactor ;
return new Ellipse ( circle . getColor ( ) , semiMajorAxis , semiMinorAxis ) ;
}
@Override
public boolean canConvert ( Shape sourceShape ) {
return sourceShape instanceof Circle ;
}
@Override
public String getConversionDescription ( ) {
return "将圆形转换为椭圆形,拉伸因子为:" + stretchFactor ;
}
/**
* 获取拉伸因子
* @return 拉伸因子
*/
public double getStretchFactor ( ) {
return stretchFactor ;
}
/**
* 设置拉伸因子
* @param stretchFactor 新的拉伸因子
*/
public void setStretchFactor ( double stretchFactor ) {
this . stretchFactor = Math . max ( stretchFactor , 1.0 ) ;
}
}