如何使用 tkz-euclide 用颜色填充多边形 1 和多边形 2 之间的区域?

如何使用 tkz-euclide 用颜色填充多边形 1 和多边形 2 之间的区域?
\documentclass{standalone}
\usepackage{tkz-euclide}
   
\begin{document}
\begin{tikzpicture}[scale=1]
    
%polygon1
\tkzDefPoint(0,0){P1}
\tkzDefShiftPoint[P1](4,0){P2}
\tkzDefShiftPoint[P2](0,4){P3}
\tkzDefParallelogram(P1,P2,P3)
\tkzGetPoint{P4}
\tkzDrawPolygon(P1,P2,P3,P4)
    
%polygon2
\tkzDefPoint(1,1){Q1}
\tkzDefShiftPoint[Q1](2,0){Q2}
\tkzDefShiftPoint[Q2](0,2){Q3}
\tkzDefParallelogram(Q1,Q2,Q3)
\tkzGetPoint{Q4}
\tkzDrawPolygon(Q4,Q3,Q2,Q1)
    
%how to fill the area between polygon 1 
%and polygon 2 with color using tkz-euclide?
    
\end{tikzpicture}
\end{document}

答案1

实际上,解决方案是使用的,但几天后版本 4TikZ就会推出解决方案。tkz-euclide

版本 2 仅tkz-euclide

\documentclass{standalone}
\usepackage{tkz-euclide}
  
  \tikzset{reverseclip/.style={insert path={
  (current bounding box.south west) --(current bounding box.north west)
   --(current bounding box.north east) --  (current bounding box.south east)
   -- cycle} }}
\makeatletter
\newif\iftkzClipOutPoly% 
\pgfkeys{tkzclippolygon/.cd,
       out code/.is if         = tkzClipOutPoly,
       out/.code               = \tkzClipOutPolyfalse}   

\def\tkzClipPolygon{\pgfutil@ifnextchar[{\tkz@ClipPolygon}{\tkz@ClipPolygon[]}}  
\def\tkz@ClipPolygon[#1](#2,#3){%
\tkzClipOutPolytrue
\pgfqkeys{/tkzclippolygon}{#1}%   
\iftkzClipOutPoly
\clip (#2)
   \foreach \pt in {#2,#3}{--(\pt)}--cycle[reverseclip];
\else
   \clip(#2)
    \foreach \pt in {#2,#3}{--(\pt)}--cycle[reverseclip] ;
\fi 
}%
\makeatother
\begin{document}

\begin{tikzpicture}[scale=1]
%polygon1
\tkzDefPoint(0,0){P1}
\tkzDefShiftPoint[P1](4,0){P2}
\tkzDefShiftPoint[P2](0,4){P3}
\tkzDefParallelogram(P1,P2,P3)
\tkzGetPoint{P4}

%polygon2
\tkzDefPoint(1,1){Q1}
\tkzDefShiftPoint[Q1](2,0){Q2}
\tkzDefShiftPoint[Q2](0,2){Q3}
\tkzDefParallelogram(Q1,Q2,Q3)
\tkzGetPoint{Q4}
\tkzDrawPolygon(P1,P2,P3,P4)
\begin{scope}
   \tkzClipPolygon[out](Q1,Q2,Q3,Q4)
  \tkzFillPolygon[blue!10](P1,P2,P3,P4)
\end{scope}

\tkzDrawPolygon(Q1,Q2,Q3,Q4)
\end{tikzpicture}
\end{document}

带圆圈

\documentclass[tikz,border=2cm]{standalone}
\usepackage{tkz-euclide}
\begin{document}

\begin{tikzpicture}
   \tkzDefPoint(0,0){O}
    \tkzDrawCircle[R](O,4)
    \tkzDrawCircle[R](O,2)
\begin{scope}
  \tkzClipCircle[out,R](O,2)
  \tkzFillCircle[R,teal](O,4)
\end{scope}

\end{tikzpicture}
\end{document}

版本 1

 \documentclass{standalone}
\usepackage{tkz-euclide}
  
  \tikzset{reverseclip/.style={insert path={
  (current bounding box.south west) --(current bounding box.north west)
   --(current bounding box.north east) --  (current bounding box.south east)
   -- cycle} }}
\begin{document}

\begin{tikzpicture}[scale=1]
%polygon1
\tkzDefPoint(0,0){P1}
\tkzDefShiftPoint[P1](4,0){P2}
\tkzDefShiftPoint[P2](0,4){P3}
\tkzDefParallelogram(P1,P2,P3)
\tkzGetPoint{P4}

%polygon2
\tkzDefPoint(1,1){Q1}
\tkzDefShiftPoint[Q1](2,0){Q2}
\tkzDefShiftPoint[Q2](0,2){Q3}
\tkzDefParallelogram(Q1,Q2,Q3)
\tkzGetPoint{Q4}
\tkzDrawPolygon(P1,P2,P3,P4)
\begin{scope}
  \clip (Q1)-- (Q2) -- (Q3) -- (Q4) -- cycle [reverseclip]; 
  \tkzFillPolygon[blue!10](P1,P2,P3,P4)
\end{scope}

\tkzDrawPolygon(Q1,Q2,Q3,Q4)
\end{tikzpicture}
\end{document}

在此处输入图片描述

在此处输入图片描述

答案2

那么,如果没有 tkz-euclide,代码更少,结果更多,会怎么样呢?

\documentclass[border=5mm]{standalone}

\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
        %squares
    \filldraw[cyan!50] (0,0) rectangle (5,5);
    \filldraw[white] (1,1) rectangle (4,4);
    
    \filldraw[cyan!50] (0,-6) rectangle (5,-1);
    \filldraw[white] (1,-4) rectangle (3,-2);
    %circles
    \filldraw[cyan!50] (8,2.5) circle (2.5);
    \filldraw[white] (8,2.5) circle (1.5);
    
    \filldraw[cyan!50] (8,-3.5) circle (2.5);
    \filldraw[white] (7,-3) circle (1);
    
    \end{tikzpicture}
    
\end{document}

在此处输入图片描述

如果你还想要边框:

在此处输入图片描述

你稍微修改一下代码:

%squares
\filldraw[fill=cyan!50,line width=1pt] (0,0) rectangle (5,5);
\filldraw[fill=white,line width=1pt] (1,1) rectangle (4,4);

相关内容